Undefined variable in workspace (GUI - Simulink)
1 view (last 30 days)
Show older comments
Hi, I would like to ask for advise on an error encountered while making a GUI linked to a Simulink model.
I created a model that calculates some data and I placed ToWorkspace blocks in every end to transfer the results to the workspace and be used in the GUI script. In the script, at the pushbutton callback, I tried this code
function pushbutton1_Callback(hObject, eventdata, handles) %#ok
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set_param('samplemdl','SimulationCommand','start');
workspace = get_param('samplemdl', 'ModelWorkspace');
myValue = getVariable(workspace,'var1');
set(handles.edit1, 'string', myValue);
but when I run the simulation from the GUI, I get this error,
Error using samplegui>pushbutton1_Callback (line 151)
Undefined variable 'var1' in workspace ('samplemdl').
so, I tried this (even though hundreds of discussions suggest not to use this)
get_param('samplemdl', 'ModelWorkspace');
myValue = evalin('base','var1');
set(handles.edit1, 'string', myValue);
and the values from workspace is shown in the edit box. but when I changed some input values and run the simulation, the values in the edit boxes didn't change but on the model the values in the display blocks changed.
Please help, what is the correct way of doing this? Thanks.
0 Comments
Answers (0)
See Also
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!