0

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.

4
  • 1
    Can you please fix the formatting of your post? You can see /editing-help for help on how to do so. Commented Jan 6, 2023 at 2:49
  • Does this answer your question? Invoke MATLAB executable with parameter from VBA in Excel Commented Jan 6, 2023 at 7:33
  • @XZhang: Unfortunately, I don't know how should I pass the excel path as a parameter to Matlab during calling. Commented Jan 10, 2023 at 5:40
  • The idea of creating a MATLAB.Application object is to pass commands into it directly. The initialization of the object creates a matlab instance in the background. But then you use a shell command to run matlab, which creates a new instance. use this to execute commands in the created instance (there is a VBA example): de.mathworks.com/help/matlab/ref/execute.html Commented Jan 10, 2023 at 7:13

1 Answer 1

0

You could change the Matlab code to have an input argument at the beginning of the function e.g. "excelPath" and then change the VBA code for the matlabCommand string to include the parameter as an input argument for the MatLab function. Maybe something that looks like this?

matlabCommand = "matlab -nodisplay -nosplash -nodesktop -r "" run('" & fileToRun & "'," & excelPath & ");exit;"" "

The & concatenates the excelPath variable as an input argument and if you use this variable in the Matlab code instead of the path you defined already it might help fix it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for your comment. I have tried to follow your suggestion, but unfortunately, it didn’t work. The steps that I considered: 1) I changed my MATLAB file to the function that could take “excelpath” as an input. 2) I defined a parameter and named it excelpath in VBA 'Dim PathCrnt As String ' ' Dim excelPath As String' 'PathCrnt = ActiveWorkbook.Path' % gives us the address of our folder that contains the Excel file ' excelPath = PathCrnt & "\main_File.xlsm" ' ' fileToRun = PathCrnt & "\ter_main.m"'
@ BoilingT 3) Change the VBA code for the matlabCommand string matlabCommand = "matlab -nodisplay -nosplash -nodesktop -r "" run('" & fileToRun & "'," & excelPath & ");exit;"" " But here ‘&’ won’t concatenate the excelPath variable as an input argument. Instead, it will connect the “fileToRun” and “excelpath” together. fileToRun is the address of our MATLAB file, and “excelpath” is the address of our excel sheet, which will concatenate these two addresses together

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.