How can I know and set Callbacks I created by functions??

조회 수: 1 (최근 30일)
Jethro
Jethro 2012년 1월 3일
I previously asked how to create Callbacks for buttons that are automatically generated by functions... I found on the guide how to do it...
Now, I have to know if callbacks where created and how to set them... I post my code
function E0_creapb(institute, fig)
UNI=unique(institute);
for i=1:length(UNI)
b=['Institute' char(UNI(i))];
c=['PB_Inst_' char(UNI(i))];
end
N_PB = length(UNI); %numb of buttons
bh = zeros(N_PB,1);
%dimension of buttons
w=104;
h=22;
for k=1:N_PB
a=['Institute ' char(UNI(k))];
z=['PB_', b];
bh(k) = uicontrol(fig, 'Style', 'pushbutton', 'Tag', ...
z, 'Units', 'pixel', 'Position', ...
[(50+(w*1.5*(k-1))) 500 w h], 'String', a);
set(bh(k), 'Callback', {c})
end
This is the function that generate my push buttons and the last "set" generate callbacks... Can anyone help me???

채택된 답변

Walter Roberson
Walter Roberson 2012년 1월 3일
Your loop initializing b and c keeps overwriting those variables.
When you set a callback to be a cell array, if the first entry is a function handle then that is the function to execute, and if the first entry is a string then the string will be evaluated as a function name. Are you sure you want to use PB_Inst_* as the name of the function to call?
  댓글 수: 4
Jethro
Jethro 2012년 1월 3일
Ok, I try ;) and what's src??
Walter Roberson
Walter Roberson 2012년 1월 3일
http://www.mathworks.com/help/techdoc/creating_plots/f7-55506.html

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

추가 답변 (1개)

Jethro
Jethro 2012년 1월 4일
I have an error to fix... When I wrote
set (bh(k), 'Callback', c{k})
Matlab says to me that "c{k}" must be a string... How can I fix it?? It's the solution I need! And, once I set callbacks, how can I recall them in my code?? ;)
EDIT
I used this code, sure that 'z' is what I want
for k=1:N_PB
a=['Istituto ' char(UNI(k))];
z=['PB_', b];
bh(k) = uicontrol(fig, 'Style', 'pushbutton', 'Tag', ...
z, 'Units', 'pixel', 'Position', ...
[(50+(lung*1.5*(k-1))) 500 lung alt], 'String', a);
set(bh(k), 'Callback', z)
end
Now, I think it works but I don't know how to recall my callback in my guide .m file.. Help me plz!! :(
EDIT 2
I tried something like
global istituto
UNI=unique(istituto);
for s=1:length(UNI)
c=['PB_Ist_' char(UNI(s))];
set(handles.(c), 'visible', 'off');
end
but I have some errors in the set, because it can't find the handles... How can I do?? I need to recall handles in a generic way in the main .m file... Can you help me??
EDIT 3:
I tried to insert a function with one of the handles names that my function should give to me, but I have an error: it says there are no fields with that name... Can anyone help me???
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 1월 8일
When you create a control in code, it is not automatically added to handles. You have to add it to handles yourself, such as
handles.(c) = b(k);
and then before the end of that routine,
guidata(bk(1), handles);
In my opinion, it is not good program design to use a different routine name for each button. How are you going to create the routines? In the form you are using, a string for the callback, the string must be the name of a function known to the base workspace (i.e., it must match the name of an existing .m file), or else (in theory) the string must be the name of a function handle stored in the base workspace. Are you planning on having your code write out individual .m files matching the names of the callbacks you are generating, or are you planning on creating those .m files before the run and hoping that you can match up the functionality of the pre-created .m files with the functionality you need for the individual pushbuttons ?
Things are much easier if you have a single callback routine and you pass to it a parameter the context it is being invoked in -- code such as I showed before.
Anyhow...
It is not clear what you mean by "recall" the callback. You can get() the 'Callback' property of the handle. This does have uses, but it is not the most common thing to do. I have been coming to find that if I am get()'ing the Callback of a routine for any reason other than to simulate that control having been invoked by the user, then I am probably creating something that is difficult to maintain and should be handled some other way.

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by