Clear Filters
Clear Filters

Can a Simulink function work with strings?

14 views (last 30 days)
Hi! I'm trying to pass a string from workspace to Simulink, in case that it is possible, can a Simulink Function work with string variables?

Accepted Answer

David Ding
David Ding on 25 May 2017
Hi Celia,
Unfortunately it is not possible for any Simulink block to accept a string variable. Simulink is used for generating numeric outputs for a model based on the numeric inputs of the model in the previous time step plus the time step size multiplied by the derivative of the model. Clearly, there is no way to perform arithmetic operations or take the derivative of a string variable.
David Ding
David Ding on 26 May 2017
Hi Walter,
I should have clarified. If Celia is meaning to ask if one can pass string variables between blocks, the answer would be no. Serial blocks and TCP blocks that work with uint8 data would otherwise be different.
Walter Roberson
Walter Roberson on 26 May 2017
Let me phrase it this way:
If I have a discrete model and at some point it calculates a block output to be uint8 [104 101 108 108 111], and that block is connected to another block with matching data types, then is it ever the case that the second block will receive something that is not bitwise identical to uint8 [104 101 108 108 111] ?
Other, that is, then perhaps a From Workspace or From File source block whose behaviour has been set to "interpolate".
Likewise for continuous models: if I have a MATLAB Function block or Level II Function block or even Arithmetic block which calculates uint8 [104 101 108 108 111] then can it ever be received by the next block as something that is not bitwise the same?

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 25 May 2017
If you are only using 8 bit characters, then you can uint8() the string and pass it as a byte vector. If you are using extended Unicode characters (for example Arabic or Chinese) then uint16() it rather than uint8(). Or you could double() the string... just not as efficient on memory.
You need to pay a bit of attention to whether the string is variable length or fixed length; fixed length is easier to deal with.
You cannot pass character vectors between blocks (unless perhaps as some form of custom signal?), but you can pass the byte values numerically and char() it inside MATLAB Function Blocks. If you do use uint8 then if you use a Serial Block and tell it to send uint8 then the receiver cannot tell the difference between that or having been sent "characters" since the serial receive just receive bytes either way.

Community Treasure Hunt

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

Start Hunting!