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
Adam
2014년 9월 26일
I've never used uisave, but it looks fine to me from a quick glance at its help page and test
Maarten
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.
Maarten
2014년 9월 26일
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
2014년 9월 26일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Variables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!