Callback and evalin
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi, my code :
function [] = P_call(varargin)
P = varargin{3};
X = get (P.po, 'value');
model_path = 'D:\MATLAB\GUI\';
scenarios_path = [model_path 'Scenarios\'];
scenarios_p = dir([scenarios_path 'TS_Sy_MooN_P_*.m']);
if ~isempty(X)
evalin('base', 'load(''default_values'')');
evalin('base', 'run(''scenarios_p(X).name'')');
evalin('base', 'run(''import_ch_inputs'')');
% sim('MooN_simu_1_2');
end
close(gcbf)
My problem : I don't know how to run this :
evalin('base', 'run(''scenarios_p(X).name'')');
Matlab don't know scenarios_p(X) etc, and I understand why, but I don't know what can I put to replace this, anybody...?
Thanks.
댓글 수: 0
답변 (4개)
Jarrod Rivituso
2011년 5월 2일
Also, not sure if you are aware of this, but you can have the Simulink model resolve variables in the caller workspace instead of the base workspace
outp = sim('MooN_simu_1_2','SrcWorkspace','current');
This might help you avoid having to do all these base workspace assignments
댓글 수: 2
K E
2012년 4월 3일
Just to be clear, your solution allow running Simulink from within a function and accessing all variables in the function's workspace, right? I haven't been able to do this previously, so this could be helpful.
Paulo Silva
2011년 5월 2일
The evalin is evaluating the argument X in the workspace but X only exists in your current function, try like this
evalin('base', ['run(''scenarios_p(' num2str(X) ').name'')']);
Sébastien Malengé
2011년 5월 2일
댓글 수: 1
Jarrod Rivituso
2011년 5월 2일
When you add parameters to a Simulink model, the model tries to resolve those parameters in the MATLAB base workspace.
When you call "sim" from within a function, it can be difficult if you still are allowing Simulink to resolve parameters in the base workspace. This is because your function has to use evalin or assignin to modify the base workspace.
Alternatively, you can define everything in your function workspace from within your function, and then call sim like I have shown to get Simulink to resolve parameters to your function workspace.
Does that make sense?
Sébastien Malengé
2011년 5월 2일
댓글 수: 2
Guy Rouleau
2012년 4월 3일
K E, you might want to look at this:
http://www.mathworks.com/support/solutions/en/data/1-ASPEIV/index.html
참고 항목
카테고리
Help Center 및 File Exchange에서 Model, Block, and Port Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!