Undefined variable in workspace (GUI - Simulink)
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(0 个)
另请参阅
类别
在 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!