Use xlsread in Simulink to implement basic parameters

3 views (last 30 days)
Dear all,
I would like to load information from an xlsx file into Simulink. The "text.xlsx"-file consits of
1 2
3 4
5 6
7 8
9 10
If if read it out in MATLAB using
A = xlsread('bedarf.xlsx');
A(1,1) would be 1, so it works well, but I am not able to include this in an MATLAB Function Simulink Model. I want to use the data as basic parameters, so I have to read them in only once per simulation. My idea, which does not work:
function A = test
persistent A_
if isempty(A_)
A_ = xlsread('test.xlsx');
end
A = A_;
The occuring problems:
The function 'xlsread' is not supported for standalone code generation. See the documentation for coder.extrinsic to learn how you can use this function in simulation.
Function 'test' (#247.61.81), line 5, column 9:
"xlsread('test.xlsx')"
Launch diagnostic report.
Component: MATLAB Function | Category: Coder error
and
Undefined function or variable 'A_'. The first assignment to a local variable determines its class.
Function 'test' (#247.92.94), line 8, column 5:
"A_"
Launch diagnostic report.
Component: MATLAB Function | Category: Coder error
I would be very happy to get a working solution and a common understanding of my mistake.
Best regards, Michael
  1 Comment
Michael
Michael on 17 Jul 2014
Hi all,
I worked out a solution (not sure whether it is the best way of doing this):
function A = test
coder.extrinsic('xlsread');
persistent A_
if isempty(A_)
A_ = zeros(5,2);
A_ = xlsread('test.xlsx');
end
A = A_;
Do I really need to declare the variable A_ as zeros(5, 2) or is there the possibility to recieve this Information automatically from the file?
Best regards, Michael

Sign in to comment.

Accepted Answer

Andy Sonnenburg
Andy Sonnenburg on 28 Jul 2014
Edited: Andy Sonnenburg on 28 Jul 2014
You should be able to remove the line containing
A = zeros(5, 2);
if you are willing to use variable-sized arrays not constrained to double type.
If you do not mind variable-sized arrays if double type is inferred, the following may be a suitable solution.
function A = test
coder.extrinsic('xlsread');
persistent A_
if isempty(A_)
coder.varsize('A_');
A_ = zeros(1, 1);
A_ = xlsread('test.xlsx');
end
A = A_;
The generated code will contain uses of emxArray_real_T, both for the storage of A_ and as the result type of test.
If variable sizing must be completely removed, then the .xlsx file will need to be read as part of code generation (though it will still be reread when the generated MEX-function is first run). If you are building using the codegen command, this can be scripted.
test_size = size(xlsread('test.xlsx'));
save test.mat test_size;
codegen test;
However, test will need to be modified to load the test.mat file at compile-time.
function A = test
coder.extrinsic('xlsread');
persistent A_
if isempty(A_)
workspace = coder.load('test.mat');
A_ = zeros(workspace.test_size(1), workspace.test_size(2));
A_ = xlsread('test.xlsx');
end
A = A_;
  1 Comment
Ryan Livingston
Ryan Livingston on 28 Jul 2014
Since you're using the MATLAB Function Block in Simulink, variable-sized data must have fixed upper bounds when generating code. So if using the coder.varsize approach you will need to specify upper bounds:
coder.varsize('A_', [N,M]);
where N and M are appropriate constants that you know will be upper bounds for the dimensions of the data being read such as [10,13].

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!