How can I quickly refer to various object handles in my GUI?
7 views (last 30 days)
Show older comments
Hello!
I am currently making a GUI in which you graphically select data from one plot and use it to produce an image.
I have 5 different pushbuttons which essentially do the same thing: allow for selection of a plotting range using ginput, display image over the corresponding data range (using imagesc) and update two edit text fields with the values obtained from ginput. However, I want each button to plot to a different set of axes (i.e. 5 different axes), and update different edit text fields.
Pushbutton handles are pbhTW1 ... pbhTW5, and they each have a 'Tag' of '1' ... '5' for reference. Axes for plotting the images are ahTW1 ... ahTW5. There are two edit text fields accompanying each image, handles ethTW1a, ethTW1b ... ethTW5a, ethTW5b.
I am currently achieving this using the following callback function, which is executed when each pushbutton is pressed:
function TWOut = TWPlot(hObject,~)
X=ginput(2);
if X(1)<X(2),
WinStart=floor(X(1));
WinStop=floor(X(2));
else WinStart=floor(X(2));
WinStop=floor(X(1));
end
TWOut=zeros(PixX*PixY,1);
h=waitbar(0, ['Plotting from channel ' num2str(WinStart) ...
' to channel ' num2str(WinStop) '...']);
steps=WinStop-WinStart;
for i=WinStart:WinStop,
fseek(fid,20+(i-1)*4,'bof');
im=fread(fid,inf,'int32',(TCSPCChannels-1)*4);
TWOut=TWOut+im;
step=i-(WinStart-1);
waitbar(step/steps)
end
close(h)
TWOut=reshape(TWOut,PixY,PixX)';
clear im
Ref = get(hObject, 'Tag');
if Ref == '1',
set(fh,'CurrentAxes',ahTW1);
imagesc(TWOut)
set(ethTW1a,'String',num2str(WinStart));
set(ethTW1b,'String',num2str(WinStop));
TWOut1 = TWOut;
elseif Ref == '2',
set(fh,'CurrentAxes',ahTW2);
imagesc(TWOut)
set(ethTW2a, 'String', num2str(WinStart));
set(ethTW2b, 'String', num2str(WinStop));
TWOut2 = TWOut;
elseif Ref == '3',
set(fh,'CurrentAxes',ahTW3);
imagesc(TWOut)
set(ethTW3a, 'String', num2str(WinStart));
set(ethTW3b, 'String', num2str(WinStop));
TWOut3 = TWOut;
elseif Ref == '4',
set(fh,'CurrentAxes',ahTW4);
imagesc(TWOut)
set(ethTW4a, 'String', num2str(WinStart));
set(ethTW4b, 'String', num2str(WinStop));
TWOut4 = TWOut;
elseif Ref == '5',
set(fh,'CurrentAxes',ahTW5);
imagesc(TWOut)
set(ethTW5a, 'String', num2str(WinStart));
set(ethTW5b, 'String', num2str(WinStop));
TWOut5 = TWOut;
end
end
I would really like to tidy up all of the if statements at the end of the function. I was thinking that I could maybe use genvarname to have a condense these into a few lines, e.g.
Ref = get(hObject, 'Tag');
set(fh, 'CurrentAxes', genvarname(['ahTW' Ref]));
imagesc(TWOut)
set(genvarname(['ethTW' Ref 'a']), 'String', num2str(WinStart));
set(genvarname(['ethTW' Ref 'b']), 'String', num2str(WinStop));
genvarname(['TWOut' Ref]) = TWOut;
but from what I understand of genvarname this will not work as a variable name is not actually created using this function.
If anyone has any ideas on how I could achieve this, I would be really grateful . It's not the end of the world if it's not possible, as my current code does the job, but it would be a nice luxury and useful for future callback functions that I plan to use in my GUI.
Thank you very much!
Siân
0 Comments
Accepted Answer
Matt Fig
on 24 Aug 2012
Here is how I handle situations like these. I store all handles to a specific type in a structure with a fieldname corresponding to that type. This lets me index into the structure as needed, simplifying the code considerably. You could easily do a similar thing, even if you are using GUIDE. Just build the structure in the initialization code. Here is an example to show you what I mean:
function [] = GUI_mult_ax()
% Plot different plots according to slider location.
S.fh = figure('units','pixels',...
'position',[300 300 860 300],...
'menubar','none',...
'name','GUI_mult_ax',...
'numbertitle','off',...
'resize','off');
S.x = 0:.01:1; % For plotting.
S.ax(1) = axes('unit','pix',...
'position',[20 80 260 210]);
S.ax(2) = axes('unit','pix',...
'position',[300 80 260 210]);
S.ax(3) = axes('unit','pix',...
'position',[580 80 260 210]);
plot(S.ax(1),S.x,S.x,'r');
plot(S.ax(2),S.x,S.x.^2,'b');
plot(S.ax(3),S.x,S.x.^3,'k');
set(S.ax,{'xtick','ytick'},{[],[]})
S.pb(1) = uicontrol('style','push',...
'unit','pix',...
'position',[20 10 260 30]);
S.pb(2) = uicontrol('style','push',...
'unit','pix',...
'position',[300 10 260 30]);
S.pb(3) = uicontrol('style','push',...
'unit','pix',...
'position',[580 10 260 30]);
set(S.pb,'String','Off','callback',{@sl_call,S})
function [] = sl_call(varargin)
% Callback for all pushbuttons.
% Note that I pass in the handle structure created above.
% This could have been stored in GUIDATA or whatever.
[h,S] = varargin{[1,3]}; % calling handle and data structure.
N = find(S.pb==h); % This lets us know which pb called.
if strcmp(get(S.ax(N),'visible'),'off')
set(S.ax(N),'visible','On')
set(S.pb(N),'string','Off')
else
set(S.ax(N),'visible','Off')
set(S.pb(N),'string','On')
end
More Answers (0)
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!