How can i pass a string from simulink mask to matlab function block

4 次查看(过去 30 天)
Dear community,
I am trying to pass a string to a matlab function block in simulink using a mask over the matlab function block.
Firstly i don´t know how to define the string in the mask, secondly i don´t know how to access it afterwards in the function block code.
I am thankfull for your help.
Tom

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2022-8-9
编辑:Fangjun Jiang 2022-8-9
With R2018a (with the String Constant block) or later, you can do this, with a little extra effort.
Add a 'String Constnat' block, specify the string as StrPra, to a new model, add a Display block and connect them.
In base workspace, assign StrPra="abc". Run simulation and the Display block shows "abc".
Select the String Constant block, Ctrl+M, add an 'Edit' parameter, name it as StrPra, specify value as StrPra, save mask.
Double click the masked block, it shows parameter as StrPra, change the value of StrPra in base workspace as StrPra="xyz",
run simulatioin, the Display block shows "xyz".
Or, double click, enter "MyString", run simulation, it shows "MyString".
Now the pass of a string from mask to a Simulink block has been completed. You can use it directly by a MATLAB Function block with R2020b or later. Check the supported data types at the document.
For R2020a and earlier, you will need to use a "String to ASCII" block to convert it to interger first. At the output, use a "ASCII to String" block if needed.

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by