You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Is it possible to load an array to the simulink workspace and call it in a matlab function?
20 views (last 30 days)
Show older comments
I have made and array in 3 dimensions initialize with zeros. I would like to import it in the simulink workspace and call it from a matlab function. Is that possible?
Accepted Answer
Ameer Hamza
on 14 May 2020
If it is a numeric array, then there are several possible ways. The easiest one is to use a constant block, write the name of the 3D variable as the value of the constant block, and then attach that constant block as input to the MATLAB function block.
19 Comments
Mariana
on 14 May 2020
But is it possible to modify the values inside this array. The idea that I have in mind is to have as switch.
If the switch is active, I only the value stored in this array.
If the switch is not active, I replace the value inside the array and replace it with a new one.
If I leave it like that I would not be able to keep the new values.
Mariana
on 14 May 2020
I have attached the simulink model. The array is initialized with zeros, later it is replaced for a "new" value when it is change to writting mode.
If change it back to reading mode, I should be able to read the new value, not a zero. I dont know what I am doing wrong.
Ameer Hamza
on 14 May 2020
I couldn't see the problem with the use of 4D array and the persistent variable. I guess the problem is with the use of anonymous function inside MATLAB function block. Since you are only calling the function once, therefore, I suggest you to use directly calculate the values as variable and avoid the anonymous function.
Mariana
on 14 May 2020
But, what happens if it is not filled with zeros initially? The idea is that this 4D array will be filled before.
For proof of concept, I filled it with ones and load it as a variable in the workspace.
How can I do this?
I tried it, but I get the following error.
Unsupported input format for From Workspace block 'concept_storage/From Workspace'. Available formats are double non-complex matrix, a structure with or without time, or a structure with MATLAB timeseries as leaf nodes. All formats require the data to be finite (not Inf or NaN).
Ameer Hamza
on 14 May 2020
The array gets filled with zeros correctly. You can check this by adding a breakpoint in your function and checking the variable's value and size in the command window. You will see that it is the same as you specified.
Mariana
on 14 May 2020
Yes, the first approach works properly. But in the second model I intent to load 4D array already filled with variable values from the workspace.
I would like to replaced the values from this array.
Ameer Hamza
on 14 May 2020
You can use evalin(): https://www.mathworks.com/help/matlab/ref/evalin.html to initialize the persistent variable from the value in the base workspace.
Mariana
on 14 May 2020
new_array = evalin(initial_array);
If I understood correctly I need to use evalin to load the "initial_array" with the prefille values different from zero?
Ameer Hamza
on 14 May 2020
Yes, you will need to use evalin to load a filled array from base workspace to Simulink block. Something like this
new_array = evalin('base', 'initial_array');
initial_array is the name of the variable in the base workspace
Mariana
on 14 May 2020
I tried it, but I got the following error.
Function 'evalin' not supported for code generation. Function 'MATLAB Function1' (#95.129.159), line 7, column 15: "evalin('base','initial_array')" Launch diagnostic report.
Ameer Hamza
on 14 May 2020
Yes, this function is not supported for code generation. If you just want to do simulations, then this is not an issue. Add this line at the beginning of the MATLAB function block
coder.extrinsic('evalin')
Ameer Hamza
on 14 May 2020
It means that if you want to generate C/C++ code for your model to implement it on an external system, then it will not work. However, note that if you are implementing on an external system, then you will not be communicating with the MATLAB workspace, so in that case, you will need to generate your matrix inside the MATLAB function block instead of loading it from base-workspace.
Mariana
on 14 May 2020
Edited: Mariana
on 14 May 2020
It works when I used:
coder.extrinsic('evalin')
storage = evalin('base', 'initial_array');
However, it seems that I cant replace values from it.
storage(x2xidx(input(1)), y2yidx(input(2)), z2zidx(input(3)), h2hidx(input(4)))= new;
Error: Subscripted assignment into an mxArray is not supported for code generation.
I thought that adding the 4D array(prefilled) to the simulink workspace in Model Explorer could make it work.
Ameer Hamza
on 14 May 2020
But I am not sure whether MATLAB function block can see the variables loaded in Model explorer. The other way to load data from base workspace which does not involve evalin() is as follow
1. create an extra input parameter at the end of the input list with the same name as a variable in the workspace. For example, if this is the original definition of your function
function y1 = matlabFunction(in1, in2, in3)
and name of the variable in the base workspace is 'myVar', then change above line to
function y1 = matlabFunction(in1, in2, in3, myVar)
2. When the definition of the MTALBA function is open, you can see "Edit-Data in MATLAB toolbar. Click it, then select the myVar from the list of inputs and change its type from input to parameter.
Now you can use myVar inside your MATLAB function block, and you do not need to use evalin().
Mariana
on 14 May 2020
Thanks! It works! Instead of loading the 4D prefilled from Matlab workspace I did it from simulink.
Does that mean that I should be able to generate C/C++ code out of it?
Ameer Hamza
on 14 May 2020
Yes, If you have Simulink code, you can generate C/C++ code from your Simulink model.
More Answers (0)
See Also
Categories
Find more on Simulink Coder 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)