How to create callback function from matlab code
이전 댓글 표시
Hi everybody!
Some times ago I wrote about "how to create pushbutton using matlab code"... I solved this problem (thanks to all advice you gave me), but now, I have to create the callback function...
I post the code I wrote...
global UNI;
global c;
global bh;
UNI=unique(institute);
for i=1:length(UNI)
b=['Institute_' char(UNI(i))];
c=['PB_Ist_' char(UNI(i))];
end
N_PB = length(UNI); %number of pushbutt
bh = zeros(N_PB,1);
%pushbuttons dimension
wdt=104;
hgt=22;
for k=1:N_PB
a=['Istituto ' char(UNI(k))];
z=['PB_', b];
bh(k) = uicontrol(gcf, 'Style', 'pushbutton', 'Tag', ...
z, 'Units', 'pixel', 'Position', ...
[(50+(wdt*1.5*(k-1))) 500 wdt hgt], 'String', a);
set(bh(k), 'Callback', z)
end
When I set visibility, I use
for s=1:length(UNI)
c=['PB_Ist_' char(UNI(s))];
handles.(c)=bh(s);
set(handles.(c), 'visible', 'off');
end
Now... I have to click on a pushbutton and generate other buttons... To generate them I need the function!
Something like
function PB_Example_Callback(hObject, eventdata, handles)
but I DON'T KNOW HOW... I tried to write manually my function, but it doesn't work... Any ideas??
채택된 답변
추가 답변 (5개)
Robert Cumming
2012년 1월 16일
set ( handles.button, 'Callback', {@FunctionHandle inputArguments} );
edit example:
function test
d = dialog ( 'windowstyle', 'normal' );
uicontrol ( 'parent', d, 'style', 'pushbutton', 'string', 'push me', 'Callback', {@PB_Callback rand(10,1)}, 'position', [100 100 100 100] );
end
function PB_Callback ( obj, event, randomInput )
disp ( 'PB Called - extra arg' );
randomInput
end
댓글 수: 5
Jethro
2012년 1월 16일
Chandra Kurniawan
2012년 1월 16일
Hi, can you tell me your detailed problem?
You said 'I have to click on a pushbutton and generate other buttons'
What does it means?
Robert Cumming
2012년 1월 16일
added an example above on how to create a pushbutton wiht a callback and a single extra input argument.
Also added a link to a good FEX with lots of GUI examples which you should look at to help you learn.
Jethro
2012년 1월 16일
Robert Cumming
2012년 1월 16일
whats your variable institute?
You should just be able to add:
set(handles.(c), 'callback', {@PB_Callback c});
to the line below where you make your PB invisible...
And yes I craeted a new dialog which I put a button (pb) on. You can use gcf yes - but that might put it on a figure you dont want it on - by stipulating a new dialog you force it to where you want.
Walter Roberson
2012년 1월 16일
0 개 추천
This is bad program design, and I already explained why over in http://www.mathworks.com/matlabcentral/answers/25221-how-can-i-know-and-set-callbacks-i-created-by-functions and I already showed an alternative design that is easy to work with.
You have encountered one of the problems I set out in that previous discussion, and you are not going to be able to solve the problem as long as you are generating function names for your callbacks.
댓글 수: 2
Jethro
2012년 1월 16일
Walter Roberson
2012년 1월 16일
After having set the callback via
set(bh(k), 'Callback', {@PB_Inst, UNI(k)} )
then you have a single callback routine
function PB_Inst(src, evt, thisUNI)
fprintf('PB_Inst callback requested for "%s"\n", thisUNI);
end
You have not indicated what you want to _do_ in the callbacks, which makes it difficult to suggest code that might be appropriately dependent on thisUNI
Jethro
2012년 1월 16일
댓글 수: 5
Walter Roberson
2012년 1월 16일
In the "for i" loop you have just before lung=104, you overwrite REP and REPU each time through the loop. REPU will be left at whatever is appropriate for the last entry.
Note: whatever you think you are doing in that loop should probably be done in the 'init' case, since you have set those variables up as global variables (that thus will retain values between calls.)
Jethro
2012년 1월 16일
Walter Roberson
2012년 1월 16일
What is the current code for setting REP and REPU is? Have you tested to be sure that REPU is not of length 1 when you create it, and have you tested to be sure that REPU is not of length 1 when you use it for the 'create' loop ?
Jethro
2012년 1월 16일
Chandra Kurniawan
2012년 1월 17일
Hi,
what these buttons do when you hit them?
Do they perform similar actions?
If so,
case 'departement'
l = length(G.str);
for x = 1 : l
if str2num(get(gcbo,'tag')) == x
disp(x);
end
end
You can change 'l' with the number of your pushbuttons in 'create'
Jethro
2012년 1월 17일
댓글 수: 4
Walter Roberson
2012년 1월 17일
Suppose you know that you will have at most 99 institutes per department. Then you can calculate an indicator variable as "department code times 100 plus institute code"
And make sure that you do not overwrite DEP and DEPu in the loop.
Jethro
2012년 1월 17일
Walter Roberson
2012년 1월 17일
nUNI = length(UNI);
DEP = cell(nUNI,1);
for i = 1 : nUNI
index_inst = find(strcmp(institute, UNI(i)));
DEP{i} = index_inst * 100 + dep_number(index_inst);
end
DEPu = unique([DEP{:}]);
No overwrites in the loop, per-institute DEP is kept instead of discarded, DEP numbers build in the institute number as part of them so DEP numbers do not class between institutes, unique is done over all department numbers.
You can recover the institute number from a DEPu by dividing by 100 and taking the integer part.
Jethro
2012년 1월 18일
Jethro
2012년 1월 18일
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!