I am trying to run MATLAB through Excel. The data that need to use in MATLAB is in an Excel sheet. So I wrote a code in MATLAB that will read the data from the Excel sheet and plot the Ternary plot. Code in MATLAB is:
opts = spreadsheetImportOptions("NumVariables", 15);
opts.Sheet = "TernaryPlot1";
opts.DataRange = "A8:O20";
opts.VariableNames = ["Var1", "Var2", "Var3", "Var4", "Var5", "Var6",
"Var7", "Var8", "Var9", "Var10", "Var11", "W4", "W5", "W6",
"NormalizedResault"];
opts.SelectedVariableNames = ["W4", "W5", "W6", "NormalizedResault"];
opts.VariableTypes = ["char", "char", "char", "char", "char", "char",
"char", "char", "char", "char", "char", "double", "double", "double",
"double"];
opts = setvaropts(opts, ["Var1", "Var2", "Var3", "Var4", "Var5", "Var6",
"Var7", "Var8", "Var9", "Var10", "Var11"], "WhitespaceRule",
"preserve");
opts = setvaropts(opts, ["Var1", "Var2", "Var3", "Var4", "Var5", "Var6",
"Var7", "Var8", "Var9", "Var10", "Var11"], "EmptyFieldRule", "auto");
opts = setvaropts(opts, ["W4", "W5", "W6", "NormalizedResault"],
"FillValue", 0);
% Import the data
B = readtable("Path of my Excell folder ", opts, "UseExcel", false);
clear opts
% % Main file for ternary plot
A=table2array(B)
warning off MATLAB:griddata:DuplicateDataPoints
l=length(A);
v=0.29./sqrt(A(:,4));
figure;
colormap(jet)
[hg,htick,hcb]=tersurf(A(:,1),A(:,2),A(:,3),A(:,4));
hlabels=terlabel('Weight on First goal','Weight on Second Goal','Weight
on Third Goal');
citra3=montage(reshape(V,size(citra)),map,'Indices',3);
I am calling above MATLAB code through VBA with the code below:
Dim MatLab As Object
Dim fileToRun As String
Dim matlabCommand As String
%% Open the Matlab program
Set MatLab = CreateObject("Matlab.Application")
MatLab.Execute ("input('Please press enter to continue');")
Dim PathCrnt As String
PathCrnt = ActiveWorkbook.Path
fileToRun = PathCrnt & "name of Matlab code that I mentioned above"
matlabCommand = "matlab -nodisplay -nosplash -nodesktop -r "" run('" &
fileToRun & "');exit;"" "
Shell (matlabCommand)
End Sub
As you see in the first part of the code, I am using the specific address of my excel sheet. But this address needs to be generalized to be useable with any user. I have asked this question before here, and they recommend I pass the excel path as a parameter to Matlab during calling ( second part of code). Based on the second part of the code, the parameter that includes the address is "PathCrnt" But I don't know much about VBA. So I would appreciate it if you could help me to find a code that could pass a parameter to MATLAB with VBA.