Info

This question is closed. Reopen it to edit or answer.

How to save actual values in a GUI to next session, simple example please

1 view (last 30 days)
Hello, I have a calculation that requires a large number of parameters to be entered and not expected to change all of them in each run, so I intent to save the last values used to next run. Im using version R2010a.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
%Función para almacenar los valores de los parámetros hasta la siguiente
%ejecución del código
saveState(handles)
delete(hObject);
function saveState(handles)
state.res_time=num2str(handles.res_timeTag);
state.max_rec_fin=num2str(handles.max_rec_fin);
state.max_rec_int=num2str(handles.max_rec_int);
state.max_rec_gru=num2str(handles.max_rec_gru);
save('var_values.mat','state')
function loadState(handles)
fileName='var_values.mat';
if exist(fileName)
load(fileName)
set(handles.res_timeTag,'string',state.res_time);
set(handles.max_rec_fin,'string',state.max_rec_fin);
set(handles.max_rec_int,'string',state.max_rec_int)
set(handles.max_rec_gru,'string',state.max_rec_gru)
delete(fileName)
end
thats the code Im actually using, together wit a call to loadState in OpeningFcn,
( I simply put it at the end, not know where to put it exactly, Im taking the idea from a video posted here http://blogs.mathworks.com/videos/2010/12/10/how-to-save-and-restore-state-of-a-gui-in-matlab/), I'm not using "get" to access the values for contruction of state, because this generates errors, this works partially but the values loaded change with no reason to me, in this form for r_max values
90 -> 239.0042
80 -> 240.0042
60 -> 241.0042
More than analyse my code, can you provide a very simple example how to do it.
  2 Comments
Image Analyst
Image Analyst on 26 Mar 2013
I think it's bad practice to delete the file right after you read it in. If you want to delete it, delete it right before you call save(), not after you call load.

Answers (1)

Walter Roberson
Walter Roberson on 26 Mar 2013
What are handles.res_timeTag and so on? Are they uicontrol(), or are they values in the handle structure that you stored numeric data into? If they are uicontrol() then you should be using
state.max_rec_fin = get(handles.max_rec_fin, 'string')
and the like.
  2 Comments
Image Analyst
Image Analyst on 26 Mar 2013
what you saved was the handle itself, which will have a value like 239.0042. You did not save the value that the control with that handle had. For example, a slider may had a min of 1, a max of 100, a current value of 42, and handle of 239.0042. You need to "get" the property you want, for example the min, max, or value, and save THAT, NOT the handle value.
Francisco Angel
Francisco Angel on 26 Mar 2013
Thanks now I understand where the error came from, and this is the form in what the video tutorial explained how to do it, but when I used the command get matlab throws the error, handle not exist, so I came with this provisional solution, I think the error of not existence was related to not using guidata command, but I am actually using it... So Im very confused.

Community Treasure Hunt

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

Start Hunting!