How to create callback function from matlab code
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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??
0 commentaires
Réponse acceptée
Chandra Kurniawan
le 16 Jan 2012
Hi, Jethro
Please try my code below.
This code create 4 buttons when you press the main button.
I hope you can modify it yourself for your purpose.
If this example is not clear, please tell me later.
function GUI1(action);
global G;
if nargin == 0, action = 'init'; end
switch action
case 'init'
G.Hfig = figure('unit','pixel','position',[100 100 400 400]);
G.HButton = uicontrol(G.Hfig,'unit','pixel',...
'style','pushbutton',...
'string','Hospital1',...
'position',[25 350 75 25],...
'callback','GUI1 create');
case 'create'
str = {'Departement1','Departement2','Departement3','Departement4'};
for i = 1 : length(str);
G.DButton(i) = uicontrol(G.Hfig,'unit','pixel',...
'style','pushbutton',...
'string',str{i},...
'position',[50 350-(i*30) 75 25],...
'tag',num2str(i),...
'callback','GUI1 departement');
end
case 'departement'
switch str2num(get(gcbo,'tag')),
case 1, disp('1');
case 2, disp('2');
case 3, disp('3');
case 4, disp('4');
end
end
13 commentaires
Walter Roberson
le 16 Jan 2012
set(bh,'Visible','off'); set(dih,'Visible','on');
You are permitted to pass arrays of handles to set()
Plus de réponses (5)
Robert Cumming
le 16 Jan 2012
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 commentaires
Robert Cumming
le 16 Jan 2012
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
le 16 Jan 2012
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 commentaires
Walter Roberson
le 16 Jan 2012
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
le 16 Jan 2012
5 commentaires
Chandra Kurniawan
le 17 Jan 2012
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
le 17 Jan 2012
4 commentaires
Walter Roberson
le 17 Jan 2012
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.
Voir également
Catégories
En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!