Callback and evalin

4 ビュー (過去 30 日間)
Sébastien Malengé
Sébastien Malengé 2011 年 5 月 2 日
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.

回答 (4 件)

Jarrod Rivituso
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
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.
Jarrod Rivituso
Jarrod Rivituso 2012 年 4 月 3 日
Yes, that is what the SrcWorkspace argument does. :)

サインインしてコメントする。


Paulo Silva
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'')']);
  1 件のコメント
Sébastien Malengé
Sébastien Malengé 2011 年 5 月 2 日
Don't works (but the problem isn't the same I think).
Matlab looking for scenarios_p(X) now, and don't find it because scenarios_p is in my function too. I have this error now :
??? Error using ==> run at 76
scenario_p(1) not found.
Error in ==> Test>P_call at 121
evalin('base', ['run(''scenario_p(' num2str(X) ')'')']);
??? Error while evaluating uicontrol Callback

サインインしてコメントする。


Sébastien Malengé
Sébastien Malengé 2011 年 5 月 2 日
Okay, so my new code is :
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'')');
scenario_p = [scenarios_path scenarios_p(X).name];
evalin('base', ['run(''scenario_p(' num2str(X) ')'')']);
% sim('MooN_simu_1_2');
end
Still don't works, because now Matlab looking for scenario_p, and it's don't exist in the workspace...
How can I simply put model_path, scenarios_path and scenarios_p in the workspace ? Or save scenario_p(X) in the workspace ? (scenario_p is m file, not mat)
Thanks !
Ps : @Jarrod Rivituso : Sorry, I don't understand what you're speaking. What's the goal of your code...?
  1 件のコメント
Jarrod Rivituso
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é
Sébastien Malengé 2011 年 5 月 2 日
I find a solution, thanks.
  2 件のコメント
K E
K E 2012 年 4 月 3 日
Can you please post your solution?
Guy Rouleau
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 ExchangeModel, Block, and Port Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by