이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Is it possible to load an array to the simulink workspace and call it in a matlab function?
조회 수: 14 (최근 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일
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/293041/image.png)
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')
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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulink Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)