GUI set parameters to .mat file (loading and saving parameters)

3 次查看(过去 30 天)
Hi there,
I am working on a Matlab GUI for a Simulink model, but first I am working on a simplified version of this to get to know the commands.
I have made a simple GUI. You can insert two values (nreal and mreal) in two text boxes, which are being summed up (plus) or are being substraced (min) by toggling a radio button. The result should appear in the text box (Sum). See the figure below. There is also a "save" button, which should save the input and output to a .mat file. This file should also be loaded into the GUI and make it work again.
I am having a few problems with setting the values to a parameter, which is recognized by matlab as a parameter. I tried to save the parameters, but 'nreal', 'mreal' and 'Sum' are not being recognized as a parameter.
What am I doing wrong with the commands? I tried to use the "set" command, but that did not work properly either.
Further, is this te proper way to save the parameters with uisave?
% --- Executes just before proefload is made visible.
function proefload_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% Choose default command line output for proefload
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes proefload wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = proefload_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
function Sum_Callback(hObject, eventdata, handles)
x = handles.nreal + handles.mreal;
y = handles.nreal - handles.mreal;
set(handles.x, 'String', x);
set(handles.y, 'String', y);
% --- Executes during object creation, after setting all properties.
function Sum_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function nreal_Callback(hObject, eventdata, handles)
% Hints: get(hObject,'String') returns contents of nreal as text
nreal = str2double(get(hObject,'String'));
% Save the new nreal value
handles.nreal = nreal;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function nreal_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function mreal_Callback(hObject, eventdata, handles)
mreal = str2double(get(hObject,'String'));
% Save the new mreal value
handles.mreal = mreal;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function mreal_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes when selected object is changed in uipanel2.
function uipanel2_SelectionChangeFcn(hObject, eventdata, handles)
if hObject == handles.plus
set(handles.Sum,'string',handles.nreal + handles.mreal);
elseif hObject == handles.min
set(handles.Sum,'string',handles.nreal - handles.mreal);
end
% --- Executes on button press in save.
function save_Callback(hObject, eventdata, handles)
uisave({'nreal','mreal','Sum'},'proefload1')
% --- Executes on key press with focus on save and none of its controls.
function save_KeyPressFCN(hObject, eventdata, handles)
In the script you might notice that I tried to use x and y to determine the Sum, but that did not work properly either. So maybe someone could tell me what I am doing wrong there, too?
Thanks in advance. Maarten
  6 个评论
Adam
Adam 2014-9-26
Yes...again I looked a bit too fast at that. handles.Sum in your case is actually the handle of the uicontrol containing the Sum, I just assumed without looking that it was the result of the Sum. I edited my previous comment to correct that.
Maarten
Maarten 2014-9-26
Thank you for your help. Much appreciated! Now I can expand the functionality of the GUI.

请先登录,再进行评论。

采纳的回答

Adam
Adam 2014-9-26
编辑:Adam 2014-9-26
handles.nreal
and
handles.mreal
are numeric, you need to wrap them in num2str to put into a string as e.g.
if hObject == handles.plus
set(handles.Sum,'string', num2str( handles.nreal + handles.mreal ));
elseif hObject == handles.min
set(handles.Sum,'string', num2str( handles.nreal - handles.mreal ));
end
and the same earlier on when you use x and y.
I forget to do this ~50% of the time so I have at least come to recognise the problem to be able to fix it quickly now!!
  8 个评论
Maarten
Maarten 2014-9-26
编辑:Maarten 2014-9-26
For now it does not have to recalculate the sum automatically when the input has been changed.
Now when the GUI starts, the "+" is selected, but when I put in the values it does not calculate yet. I tried to set it with
set(handles.uipanel12,'SelectedObject',handles.plus)
In the
function proefload_OpeningFcn(hObject, eventdata, handles, varargin)
But that did not work yet. The "+" is selected, but the sum value was not calculated.
Adam
Adam 2014-9-26
Just put
set( handles.Sum, 'String', '9' )
or whatever the correct maths if for the initial values you give to n real and m real. Or hard-code it in GUIDE. Basically wherever you put the default values for n real and m real from which it would calculate the sum, just put the correct value for the sum in the same way.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by