retrieving data from a gui

13 Ansichten (letzte 30 Tage)
Priscilla
Priscilla am 14 Apr. 2011
Hello everybody.
I am new user of GUi and have a maybe stupid question. I have a main program from which I call a gui created with guide. From this Gui function (called button_ter), I would like to retrieve de data of one of the callback to use it in the main program. Here is the part written in the gui (with guide) from which I would like to extract the "lambda" value:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
uicontrol(hObject)
uiresume;
close(handles.figure1)
%delete(hObject.figure1);
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.lambda = str2double(get(hObject,'String'))
varargout{2}=handles.lambda
guidata(hObject,handles)
%handles = guidata(handles.lambda);
setappdata(handles.edit1,'lambda',varargout{2})
I tried to retrieve the data from the main program with "getappdata" in this main program but it doesn't work. After looking a lot on matlab help,I've tried many things... test=getappdata(@edit1,lambda) test=getappdata(@button_ter,lambda) test=getappdata(@button_ter,edit1)
but none worked. What I do not understand is that the data seems to be correctly generated in my gui code because I see this on the prompt:
handles =
figure1: 173.0016
edit1: 4.0017
text3: 3.0017
text2: 2.0017
pushbutton1: 1.0017
uitable1: 0.0017
text1: 174.0016
output: 173.0016
lambda: 45
varargout =
[] [45]
but after that, either it writes -Undefined function or variable -or Conversion to double from function_handle is not possible.
I'm sure I forgot something stupid in the command line from my main program.
But What? Does anyone have an idea for me?
Thanks a lot.
Scilla

Akzeptierte Antwort

Priscilla
Priscilla am 14 Apr. 2011
THANKS!!!! it works!
but I do not understand. I thought the "setappdata" just to do what you mention: store lambda even if I close the button.
so I do not understand the difference between setappdata/getappdata and set/get. Other stupid question: the "0" in the set and get is a value for the handle is that right? I was confused by this handle because I tried (as you can see in my "setappdata") to put a string and not a value.
Thanks a lot once again. I spent so many time to grab these gui things...
  2 Kommentare
Matt Fig
Matt Fig am 14 Apr. 2011
SETAPPDATA for a figure disappears when the figure is closed. SET is used to set any settable property for a specific object. The 0 is the MATLAB root. You cannot close this without closing MATLAB itself.
Oleg Komarov
Oleg Komarov am 5 Mai 2011
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
Right way to thank people for helping you out

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt Fig
Matt Fig am 14 Apr. 2011
There are many ways to do what you want here. First though, using VARARGOUT does not do what you think it does in this callback. That is because the callback has no output arguments.
To your main point, set the callback like this:
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
lambda = str2double(get(hObject,'String'))
set(0,'userdata',lambda);
.
.
Now from in your main function, use:
lambda = get(0,'userdata')
After the GUI is closed. The reason why you want to store the data outside the GUI is that your pushbutton closes the figure. So any data stored anywhere in the figure goes away with it.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by