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

3 views (last 30 days)
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

Answers (1)

Fangjun Jiang
Fangjun Jiang on 9 Aug 2022
Edited: Fangjun Jiang on 9 Aug 2022
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.

Community Treasure Hunt

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

Start Hunting!

Translated by