Use of Variables in Simulink Matlab Function
Afficher commentaires plus anciens
Hi,
I'd like run a complex code in a Matlab Function Block in Simulink. The initialization of some variables via another matlab script works fine, so some objects (e.g. the double h =1 ) the are loaded to the Model Workspace. (they appear in Model Explorer)
When running the Simulink Model including the Matlab Function Block, h is unknown.
function y = fcn(u)
y = u *h;
Error: Undefined function or variable 'h'. Function 'MATLAB Function1' (#66.156.157), line 6, column 8: "h"
How to read and write Variables from Model Workspace in the Matlab Function Block?
Thanks!
Réponses (1)
Fangjun Jiang
le 10 Jan 2022
1 vote
In MATLAB Function block Editor, click "Data", define "h" as a parameter.
7 commentaires
Kilian Lasch
le 11 Jan 2022
Ok fine, thank you. I know manage to use variables from Workspace, which i defined as simulink.parameter in the initialization file. But how can i overwrite them in the Matlab function block? Not as an output, but directly in Model Workspace.
For example, i'd like to change the variable h permanentely if the input u statisfies a certain condition (once).
Thanks in advance!
Fangjun Jiang
le 11 Jan 2022
For that, you can use evalin(), assignin(), evalinGlobalScope(), etc.
Kilian Lasch
le 13 Jan 2022
Using evalin() or assignin() in the matlab function block stops with the error:
Function 'evalin' not supported for code generation.
Fangjun Jiang
le 13 Jan 2022
That is true. If you require code generation, then you need to re-consider this approach.
There might be a way. Create h as a Simulink.Parameter object in the base or model workspace. Still define h as a parameter for the MATLAB Function block. Inside, you can change the value of h by setting h.Value. You don't need to make it an output. Outside, use Data Store Memory block to link h, use Data Store Read block to read its value to see if the value changed inside the MATLAB Function block is reflected.
Kilian Lasch
le 14 Jan 2022
I initialized h as double as Simulink.Parameter in Model Workspace and added a Data Store Memory block as well as a Data Store Read block. In the Function Block i change h via h.Value.
If i define h as Scope Parameter in Matlab Function Block, i recieve the error
Attempt to extract field 'Value' from 'double'.
Function 'MATLAB Function1' (#24.195.196), line 7, column 5:
"h"
Defining h as Scope Data Block Memory, the error does not occur but neither the Workspace Parameter h nor the h in Data Store Read block changes.
Wouldn't using Data Store Read and Write blocks at the output and input of the function block be a solution? Would this also allow to store variable sized arrays?
Fangjun Jiang
le 14 Jan 2022
If there is no constraint to add input and output to the MATLAB Function block to read and write the value, it certainly makes it easier.
I believe it can store variable size data. Just need to set the proper property values.
Fangjun Jiang
le 14 Jan 2022
Look at the "Example - Incorrect". It is "incorrect" in terms of style guideline but it works in terms of functionality.
Catégories
En savoir plus sur Sources dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!