MATLAB to excel to MATLAB

2 views (last 30 days)
Jarod Schmidt
Jarod Schmidt on 4 Nov 2019
Edited: Roofus Milton on 4 Nov 2019
Hello,
I'm trying to write an input into an excel file which will then calculate the the values I want and then read the values of the excel sheet back into matlab. I keep getting NaN for the outputs of the excel sheet and was wondering what I would need to do in order to obtain the values I want.
Code
filename = 'test3.xlsx';
Den = {'=PropsSI("Dmass","T",850,"P",10000000,"HEOS::CarbonDioxide")'};
HeC = {'=PropsSI("C","T",850,"P",10000000,"HEOS::CarbonDioxide") '};
vis = {'=PropsSI("V","T",850,"P",10000000,"HEOS::CarbonDioxide") '};
xlswrite('test3.xlsx',Den,1,'B2')
xlswrite('test3.xlsx',HeC,1, 'B3')
xlswrite('test3.xlsx',vis,1,'B4')
values = xlsread('test3.xlsx');

Answers (1)

Roofus Milton
Roofus Milton on 4 Nov 2019
Edited: Roofus Milton on 4 Nov 2019
Hi Jarod-
I do not have the add-in which defines the formula PropsSI. I have provided a generic way to pass data to and return data from Excel. I suspect the error in your approach is the result of the file never being opened. The commented lines with your formulas should replace my formulas.
try
%% Get the Excel Application
try
% Try to attach to an open Excel application
excel = actxGetRunningServer('Excel.Application');
catch exception
% Create the COM object
excel = actxserver('Excel.Application');
end
%% Get the workbook and the worksheet
% Ensure the object is visible
excel.Visible = true;
% If there are no open workbooks then add one to the collection
wb = excel.Workbooks.Add();
% Get the active worksheet in the active workbook
ws = wb.ActiveSheet;
ws.Range("A1").Value2 = 'From MATLAB';
ws.Range("A2").Formula = '=10';
ws.Range("A3").Formula = '=11';
ws.Range("A4").Formula = '=12';
%ws.Range("A2").Formula = '=PropsSI("Dmass","T",850,"P",10000000,"HEOS::CarbonDioxide")';
%ws.Range("A3").Formula = '=PropsSI("C","T",850,"P",10000000,"HEOS::CarbonDioxide")';
%ws.Range("A4").Formula = '=PropsSI("V","T",850,"P",10000000,"HEOS::CarbonDioxide")';
output = cell(3, 1);
output{1, 1} = ws.Range("A2").Value2;
output{2, 1} = ws.Range("A3").Value2;
output{3, 1} = ws.Range("A4").Value2;
catch exception
rethrow(exception)
end

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!