Using assignin with structure elements

조회 수: 44 (최근 30일)
John F
John F 2020년 1월 4일
댓글: John F 2020년 1월 6일
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!

채택된 답변

per isakson
per isakson 2020년 1월 4일
"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')
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 1월 4일
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 2020년 1월 6일
Thanks folks!. The combo of these two helped a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by