필터 지우기
필터 지우기

Change variables in the base workspace through a Matlab Function block

조회 수: 57 (최근 30일)
Lisa P
Lisa P 2023년 9월 21일
댓글: Jon 2023년 10월 2일
Hi,
I'm trying to change a matrix (of type double) in the base workspace through a Matlab Function block in Simulink. To access the matrix in the base workspace within the Matlab function block, I have set the variable type in the Symbols pane of the Matlab function block to parameter data. Then I change the parameter and assign it to the base workspace with the assignin function, which I found in past solutions for the same problem.
Here is an example of my problem:
function updateBaseWorkspaceVariable(Workplan, TasksToDo)
% Modify the base workspace variable using the parameters
assignin('base', 'myVariable', Workplan + TasksToDo);
end
However when running my code and this example code I get the following error message:
Error:Function 'assignin' not supported for code generation.
Function 'MATLAB Function' (#343.124.176), line 3, column 5:
"assignin('base', 'myVariable', Workplan + TasksToDo)"
Launch diagnostic report.
Since this is a solution already used in past Matlab versions I'm wondering, if it's a problem within the Matlab version that I'm using (23a) or whether there is something I'm missing.
Thanks in advance for your help!
Kind regards
Lisa

채택된 답변

Jon
Jon 2023년 9월 21일
편집: Jon 2023년 9월 21일
I would suggest using the "Interpreted MATLAB Function" block for this purpose. It doesn't need to get compiled. It is under
Simulink>User-Defined Functions in the Library Browser.
In the Parameters field MATLAB function, enter something like myfun(u).
In a directory that is on the MATLAB path, make and save an .m file called myfun(u), that has your code it in it, e.g.
function y = myfun(u)
y = u^2;
assignin('base', 'y',y);
  댓글 수: 5
Lisa P
Lisa P 2023년 9월 22일
Hi Jon,
Thank you for the quick reply!
Both solutions work perfectly.
Jon
Jon 2023년 10월 2일
Hi Lisa, if you are all set, please accept an answer so others will know that an answer is available. Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Harald
Harald 2023년 9월 21일
Hi Lisa,
you can declare a function as extrinsic to address the error message, using coder.extrinsic.
I would question the workflow of writing to the base workspace every time the block is executed. If you only need to have the variable in the workspace after the simulation, you can use model callbacks.
Best wishes,
Harald
  댓글 수: 1
Lisa P
Lisa P 2023년 9월 22일
Hi Harald,
Thank you for your quick reply as well!
Also thank you for the tipp on model callbacks. I might use those in other places. For my question the writing to the base workspace at every time of the blocks execution was intended. I'm using the table in the workspace as an input for a state chart. I want the table to be updated at certain discrete points in time to symbolise tasks being added to a To-Do-List. Then there is a state in the state chart that checks whether there are tasks in the list and when there are tasks the state 'working' is reached. Otherwise the state 'waiting' remains active. Therefore I wanted the list to be updated at every timestep.
My solution might not be the best, but I was just trying to make an idea work (:
Kind regards,
Lisa

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by