Assign variable to Initialization/Current Workspace

조회 수: 2 (최근 30일)
Jason Nicholson
Jason Nicholson 2018년 3월 21일
답변: Jason Nicholson 2018년 5월 2일
In the mask of a simulink block, I need to loop over a list of variables and values assign them into the initialization workspace (not the base workspace. Touching the base workspace is unacceptable). The variables and values are stored in a MATLAB table that looks like this:
Use of eval, evalin, and assignin is close to the functionality that I need. However, I avoid use of eval and evalin as much as I can because of best practices in the MATLAB documentation. I think assignin is the functionality I need but I think I have to use it in an unusual way to get it to work correctly. Here is the code snippet that uses assignin:
helper = @(varname,value) assignin('caller',varname,value);
for iVariable=1:size(myVariableTable,1)
helper(myVariableTable.VariableName{iVariable}, myVariableTable.Value(iVariable));
end
This above code snippet works but I have to wrap assignin in an anonymous function. Is there a better way to get the functionality I need of assigning a variable to current workspace?

답변 (1개)

Jason Nicholson
Jason Nicholson 2018년 5월 2일
Yeah, don't do this. It will cause you pain. The Simulink tools that track where your variables come form cannot track where your variables come from. For instance, the Dependence Analysis in Simulink Project cannot track your where your variables come from.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by