MATLAB Answers

Using assignin with structure elements

19 views (last 30 days)
John F
John F on 4 Jan 2020
Commented: John F on 6 Jan 2020
I have created a script where I want to assign a character string to an element in a structure, namely the "description" field of Simulink data objects in the workspace. I have seutp a loop to assign each using the following command:
myVar is the variable as a string (e.g., 'name.Description'). myDes is the character string to be assigned as a description. When I run the command, I get this error:
Error using assignin
Invalid variable name "name.Description" in
Error in AddDescriptionToDDEntries (line 15)
If I perform the assignment at the command line using an "=", it works fine. What am I missing?


Sign in to comment.

Accepted Answer

per isakson
per isakson on 4 Jan 2020
"What am I missing?" assignin() cannot assign to a field of a structure.
It's possible to do it with evalin(). Compare the note of the documentation on assignin()
To assign values to specific elements of an array, use the evalin function.
evalin('base','X(3:5) = -1')


Walter Roberson
Walter Roberson on 4 Jan 2020
You can evalin to get the current structure, modify the local copy, then assignin the entire structure from the modified version.
John F
John F on 6 Jan 2020
Thanks folks!. The combo of these two helped a lot.

Sign in to comment.

More Answers (0)

Sign in to answer this question.