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

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

I've never used uisave, but it looks fine to me from a quick glance at its help page and test
True. I want to save 'nreal', 'mreal' and 'sum' to my workspace, but since Matlab does not recognize them als parameters, this save function does not work either.
Adam
Adam 2014년 9월 26일
편집: Adam 2014년 9월 26일
Ah, sorry, I didn't look closely enough. Because they are on handles, not in the workspace that won't work.
Add
nreal = handles.nreal;
mreal = handles.mreal;
Sum = str2double( get( handles.Sum, 'String' ) );
above uisave and that should work. I don't think you can just put 'handles.nreal' straight into the uisave although I haven't tried.
nreal, mreal and sum are now saved as parameters in the workspace. Still there is a problem with the sum function, because the output in the workspace is always equeal to 182.0154, no matter what input I give.
Though, the output of the sum in the GUI is correct.. Weird.
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.
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

When I wrap handles.nreal in num2str I only get errors. Could you be more specific where I should do this?
The main purpose of this file is to save the input and output values into the workspace as:
nreal = x mreal = y sum = x+y or sum=x-y
Adam
Adam 2014년 9월 26일
편집: Adam 2014년 9월 26일
Ok, I just downloaded your actual files to have a quick check. To my surprise what you had without num2str works (though adding num2str also works).
What didn't work as you mentioned is the save but I think my comment to your question should fix that.
The only other things I could see that were perhaps not ideal where that nothing stops the user typing into the field that is supposed to be for the result (not a huge problem if whoever uses it simply doesn't do that) and that the result does not update when you change the inputs only when you switch the radio button.
You are correct, I should do something about that update when you change the inputs.
I think the problem is the sum function, which are defined as:
x = handles.nreal + handles.mreal;
y = handles.nreal - handles.mreal;
set(handles.x, 'String', x);
set(handles.y, 'String', y);
But I don't use them anywhere, because when I delete this, the program still functions. I should replace
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
the "handles.nreal + handles.mreal" part by the function, defined as "x" and "handles.nreal - handles.mreal" party by the function "y". But how?
You can probably just remove your whole Sum_Callback.
When it comes to callbacks you really just have to ask yourself "Do I need something to react immediately when this changes or will I only need the result when I click some other button - i.e. trigger some other callback".
In your case as the GUI stands you do not need to store the Sum result every time it updates as the only time you care what its result is is when you click the save button, at which point you can extract the Sum direct from the uicontrol as my (now corrected I hope!!) comment to your original answer demonstrates.
If you expand your GUI functionality and do need something to react every time the Sum changes then you will need the callback.
How can I make it select option "+" and automatically calculates the Sum, when starting the GUI? Because now it will only calculate the sum when I select the "-" first.
Since that is a one-off case you might as well just hard-code it in if you are hard-coding in default values for the sum anyway.
If you want it to also update when you change one of the numbers but not the '+' or '-' you will need a callback for the editboxes where you enter those numbers but for the initialisation case there is no point over-engineering it if you are hard-coding default values anyway and the default initial result is equally fixed.
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.
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개)

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

질문:

2014년 9월 26일

댓글:

2014년 9월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by