現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Is it possible to load an array to the simulink workspace and call it in a matlab function?
22 ビュー (過去 30 日間)
古いコメントを表示
Mariana
2020 年 5 月 14 日
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?
採用された回答
Ameer Hamza
2020 年 5 月 14 日
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 件のコメント
Mariana
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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')
Mariana
2020 年 5 月 14 日
That means that if I want to build the model and compile. I wont be able to?
Ameer Hamza
2020 年 5 月 14 日
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
2020 年 5 月 14 日
編集済み: Mariana
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
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
2020 年 5 月 14 日
Yes, If you have Simulink code, you can generate C/C++ code from your Simulink model.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)