Using assignin with structure elements
43 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
John F
el 4 de En. de 2020
Comentada: John F
el 6 de En. de 2020
Hi,
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:
assignin('base',myVar,myDes)
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
ASSIGNIN.
Error in AddDescriptionToDDEntries (line 15)
assignin('base',myVar,myDes)
If I perform the assignment at the command line using an "=", it works fine. What am I missing?
Thanks!
0 comentarios
Respuesta aceptada
per isakson
el 4 de En. de 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')
I assume that you read TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)
2 comentarios
Walter Roberson
el 4 de En. de 2020
You can evalin to get the current structure, modify the local copy, then assignin the entire structure from the modified version.
Más respuestas (0)
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!