Use xlsread in Simulink to implement basic parameters

4 次查看(过去 30 天)
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 个评论
Michael
Michael 2014-7-17
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

请先登录,再进行评论。

采纳的回答

Andy Sonnenburg
Andy Sonnenburg 2014-7-28
编辑:Andy Sonnenburg 2014-7-28
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 个评论
Ryan Livingston
Ryan Livingston 2014-7-28
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].

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by