Get figure handles in matlab GUI
Show older comments
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
Geoff
on 10 May 2012
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.
Khoi Nguyen
on 10 May 2012
Walter Roberson
on 11 May 2012
Khoi, search on tag:always-parent
Answers (1)
Walter Roberson
on 10 May 2012
0 votes
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
Khoi Nguyen
on 10 May 2012
Walter Roberson
on 11 May 2012
'Axes' is not a 'Style', it is a 'Type'. 'Style' is used to distinguish different uicontrol.
Khoi Nguyen
on 23 May 2012
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!