Reading from GUI edit boxes using a self made function

조회 수: 2 (최근 30일)
Luka Drobilo
Luka Drobilo 2012년 11월 9일
Hello!
I'm creating a GUI application which contains, among other things, several edit boxes tagged joint1 through joint7. Since I don' want to have to read separately from each of them every time, I created a read function. First I defined a variable from which they'd be called in the main program:
internal_coord=['joint1';'joint2';'joint3';'joint4';'joint5';'joint6';'joint7']
and the function as:
function status=read(object)
k=size(object);
d=k(2);
k=k(1);
for i=0:k-1
adr=object(i+1,1:d);
status(i+1)=str2num(get(handles.(adr),'String'))
end
which would be called as read(internal_coord). I've tried this in the main program and it works normally, but when I try using the function it writes out an error: "Undefined variable "handles" or class "handles.joint1". I must admit that I don't know much of how handles are defined, but I presume the problem is that the function can't reach the edit boxes.
Any ideas how to solve that?
Luka Drobilo

채택된 답변

Matt Fig
Matt Fig 2012년 11월 9일
편집: Matt Fig 2012년 11월 9일
Where is that function located? Is it in the GUI m-file or a separate m-file? From where are you calling it? Are you calling it from a callback?
If the function is in the GUI m-file and you are calling it from the callback of, say, a pushbutton, then simply amend the function like this:
function status=read(object,handles)
Then when you call it from the callback, pass the object arg and the handles.
  댓글 수: 1
Luka Drobilo
Luka Drobilo 2012년 11월 9일
The function is located in a separate m-file, but I tried adding the extra "handle" argument to the function and it works now.
Thanks a lot! :)

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

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2012년 11월 9일
Hi Luka,
I see two possibilities for the behavior you are seeing:
  1. Are you running the *.m file or just opening the figure? You have to run the *.m file, opening the figure directly will cause this error since the opening stuff hasn't happened.
  2. IS this code in your GUI's OpeningFcn? If it is, it is possible that the boxes have not been created yet.
I am guessing it is option 1.
  댓글 수: 1
Luka Drobilo
Luka Drobilo 2012년 11월 9일
I've tried to run it both ways: by opening the *.m file and by opening the figure, same thing.
The variable is defined in the OpenningFcn and the function is called in the OpeningFcn, as well as in some other Callbacks. But like I said, I've tried running the code directly in the main program and it works normaly (even if it is in the OpeningFcn) so they must be created, but it seems the function can't reach them.

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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by