Get figure handles in matlab GUI

Hi,
I have a question regarding getting a figure handle from an existing Matlab graph. I believe this problem is straight forward; however, cause I'm very new to Matlab Gui so I've been struggling with this for a while. The problem is as follows:
Firstly, in one matlab GUI called 'page1', given A=[2.1 3.1 4.1], I created a bar chart of A with the handle name as 'handles.water'. The label for each column in the chart is: 'Shower', 'Faucet', 'Clotheswasher' repectively. I want the first column to be in red, the second one in blue, and the third one in yellow. The code for this part is shown below:
A=[2.1 3.1 4.1];
Labels = {'Shower','Faucet','Clotheswasher'};
color_map={'r' 'b' 'y'};
for i=1:3
p(i)=bar(i,A(i),'facecolor',color_map{i});
hold on
end
hold off
set(p,'Parent',handles.water);
set(handles.water, 'XTick', 1:4, 'XTickLabel', Labels);
After running this part, I get the bar chart as desired.
Secondly, I create another GUI called 'page2'. In 'page2', I have a vector new_A=[1 2 3]. What I want is to update the existing bar chart with the new data new_A when I click a push button in 'page2'. The problem is at calling the handle of the existing bar chart in 'page1'. What I did is:
handles.water=findobj('Style','Axes','Tag','water')
And then put the same code above to create the new bar chart in place of the old one. However,anytime I do this, I always get:
handles.water=[ ]
Could anyone help me point out the problem.
Thanks,
William

3 Comments

I would pass the desired handle as a parameter when you create your second GUI, or create the first GUI from within the second GUI. Either way, store the handle inside that second GUI as UserData or whatever. I've never used findobj and can't really get the point of it from 10 seconds of skimming the help, so I can't answer your specific question.
Another option is using the 'global' keyword... But that's sort of like dropping dynamite in a paint tin to redecorate your living room.
Thanks for your quick reply Geoff. As your suggestion,I stored the desired handle created from the GUI, and then recalled it using a pushbutton in the second GUI. I also got the value for that handle in the second GUI, however, when I did the bar plot, the figure was displayed in the second GUI, not the first one
Khoi, search on tag:always-parent

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 10 May 2012
In the code you show, you have not set the Tag field to 'water' on any uicontrol. In fact in the code you show, you have not created handles.water at all.

3 Comments

Walter,
I used the matlab built-in 'guide' to create an 'axes' object, and set the tag of that object as 'water'. Could you please show me how to create a handles.water using matlab code and assign the bar chart to that handle.
Thanks,
William
'Axes' is not a 'Style', it is a 'Type'. 'Style' is used to distinguish different uicontrol.
Walter,
Thanks for the advice. It finally works!!!

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Asked:

on 10 May 2012

Community Treasure Hunt

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

Start Hunting!