GUI set parameters to .mat file (loading and saving parameters)
5 ビュー (過去 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145532/image.png)
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 日
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.
採用された回答
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 件のコメント
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 Center および File Exchange で Migrate GUIDE Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!