MATLAB: Run a simulink/script from a pushbutton

guiguidepushbuttonsimulink

hi
i've a script in a PB_function and when i push the button it shows me a lot off errors.Running the originar script without the PB it works but when i use in PB_function doesn't work:
??? Error using ==> gui_passdata>pb2_call at 438
Error due to multiple causes.
Caused by:
Error using ==> gui_passdata>pb2_call at 438
Error evaluating parameter 'VariableName' in
'modelo_pilha1/B': Undefined function or variable 'P1'.
in the scrip i've a simulink model that uses the variable P1 but i don't change anything and it doesn't work.
a(1)=5*10^(-3);
b(1)=5*10^(-2);
PP1=a(1)+(b(1)-a(1))*rand;
P1=[t PP1];
to run the simulink model
load_system('modelo_pilha1'); % correr simulink e obter Vsimulado
sim('modelo_pilha1');

Best Answer

From workspace block reads wariable from base workapace.
If you are defining and writing P1 variable from pushbutton callback function, then it will not saved in base workspace. Every function have its own workspace and they not shares variables.
Try saving P1 in .mat file and load this .mat in base wokspace before starting the simulation.
P1=[t PP1];
save('Myfile.mat','P1');
evalin('base','load(''Myfile.mat'')');
%load model & Simulate
load_system('modelo_pilha1'); % correr simulink e obter Vsimulado sim('modelo_pilha1');