retrieving data from a gui

조회 수: 12 (최근 30일)
Priscilla
Priscilla 2011년 4월 14일
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

채택된 답변

Priscilla
Priscilla 2011년 4월 14일
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
Matt Fig
Matt Fig 2011년 4월 14일
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 2011년 5월 5일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt Fig
Matt Fig 2011년 4월 14일
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.

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by