Read data in matlab functions for protected model Simulink
Show older comments
Hello,
I developped a Simulink model in which I call some Matlab function block. In these ones, I need to use some functions such as pwd or xlsread to read an entire xlsx/csv file once and get some table to use in matlab function later.
But I also need to protect my model and to use these functions I need to pass through :
coder.extrinsic
But it is not supported for the code generation.
Is there a easy way to fix this ? I didn't find a Simulink block to get some information such as the path of the executed model, and any other reading function has the same problem as xlsread.
Thank you very much in advance for your help.
Best regards.
12 Comments
Walter Roberson
on 27 Aug 2021
The From Spreadsheet block can have code generated; https://www.mathworks.com/help/simulink/slref/fromspreadsheet.html
Mathieu Gauquelin
on 27 Aug 2021
Edited: Mathieu Gauquelin
on 27 Aug 2021
Walter Roberson
on 27 Aug 2021
Is the file constant, or does the user need to be able to change it?
If it is constant, then postings I find say to read it and store it in a .mat file, as it is possible to generate code for load() of a .mat file (which if I understand correctly pretty much gets converted into C code that assigns values to the variables -- the loading ends up getting done statically instead of by reading from the disk.)
Mathieu Gauquelin
on 27 Aug 2021
Walter Roberson
on 27 Aug 2021
Ah, if I understand the difference between load() and coder.load() https://www.mathworks.com/help/matlab/ref/load.html#bvlc6g4-1 then load() should indeed work at run-time, whereas coder.load() does what I described before of generating code for the exact set of data to be loaded.
Mathieu Gauquelin
on 27 Aug 2021
Mathieu Gauquelin
on 30 Aug 2021
Walter Roberson
on 30 Aug 2021
- coder.load loads data at compile time, not at run time. If you are generating MEX code or code for Simulink® simulation, you can use the MATLAB function load to load run-time values.
So, load() is documented as being supported for Simulink code generation. That was the case in R2016b as well, https://www.mathworks.com/help/releases/R2016b/simulink/slref/coder.load.html?searchHighlight=coder.load&s_tid=doc_srchtitle#moreabout which said "coder.load loads data at compile time, not at run time. If you are generating MEX code or code for Simulink® simulation, you can use the MATLAB function load to load run-time values."
Mathieu Gauquelin
on 31 Aug 2021
Edited: Mathieu Gauquelin
on 31 Aug 2021
Mathieu Gauquelin
on 1 Sep 2021
Walter Roberson
on 2 Sep 2021
You tell me that the load() function is not compatible to be used during a simulation ?
No, I am telling you that the documentation says that you can use load() of a .mat file at run-time, and that the file can vary (though you do need to take care about which directory to look in for the file.)
I have no information about how any of this interacts with protect()
Mathieu Gauquelin
on 2 Sep 2021
Answers (0)
Categories
Find more on Configure and View Diagnostics 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!