Save variables in gui

9 views (last 30 days)
Fiboehh
Fiboehh on 6 May 2011
Commented: Jan on 13 Feb 2018
Dear, i'm strubling with the problem to save variables in a gui. I have an edit text with nothing in and when you type a number in it, i have to use this double in another widget or another function. I wrote http://matlab.wikia.com/wiki/FAQ?cb=4562#How_can_I_share_data_between_callback_functions_in_my_GUI.3F and tried the getappdata manner. So my callback function is like:
function ls_Callback(hObject, eventdata, handles)
% hObject handle to ls (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
ls = str2double(get(hObject,'String')) %to convert to double
ls = getappdata(handles.ls ,'ls') % so i can save it?
save(sino,'ls') % gives fualt...
and to use it in another function i tried
sinopara = load(sino);
ls = sinopara.ls;
But it doesnt work, please please a understandeble solution :) thx in advance

Accepted Answer

Walter Roberson
Walter Roberson on 6 May 2011
function ls_Callback(hObject, eventdata, handles)
% hObject handle to ls (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
ls = str2double(get(hObject,'String')) %to convert to double
handles.ls = ls;
guidata(handles);
Then in any other callback or function that already had handles being passed to it, handles.ls would have the value. If you need the value someplace that does not have handles being passed to it, then
handles = guidata(YourFigureNumber);
and then use handles.ls
  2 Comments
Walter Roberson
Walter Roberson on 6 May 2011
By the way: the first thing that has to be passed to save() is a file name, but in your sample code you are passing an undefined variable.
save('sino','ls')
would have gotten you further, but your getappdata() call is incorrect so you probably would have ended up saving an empty matrix.
Jan
Jan on 13 Feb 2018
@Walter: Replace:
guidata(handles)
by
guidata(hObject, handles)

Sign in to comment.

More Answers (1)

Fiboehh
Fiboehh on 7 May 2011
I was too fast to accept the answer. I have a fault message when i try:
function ls_Callback(hObject, eventdata, handles)
% hObject handle to ls (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
ls = str2double(get(hObject,'String')) %to convert to double
handles.ls = ls;
guidata(handles);
this is the error message:
??? Error using ==> guidata at 89
H must be the handle to a figure or figure descendent.
Error in ==> DA>ls_Callback at 444
guidata(handles);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> DA at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)DA('ls_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
  1 Comment
Jan
Jan on 13 Feb 2018
Replace:
guidata(handles)
by
guidata(hObject, handles)

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!