You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
bar() to a specific axes in GUI
7 views (last 30 days)
Show older comments
Hello,
I have 3 axes in my gui, and I'm trying to plot a bar graph on one of them. I have tried several solutions; however, I have had no luck.
I may have not applied the solution properly.
https://www.mathworks.com/matlabcentral/answers/324609-assigning-plot-to-an-existing-axes-matlab-gui
The answers above indicate that I should use: axes(handles.axes1); Though I'm not really sure which function I should place it in.
Any input is really appreciated
Accepted Answer
Rik
on 18 Nov 2018
You should always use explicit handles. You could indeed use the code you describe (which you would then need to put just before the call to bar).
But there is an easier solution: use explicit handles in your call to bar. If you read the doc, you see you have two options: specifying the axes as the first argument, or using the Name-Value pair to specify the parent axes.
24 Comments
Obadah M.
on 18 Nov 2018
Thank you for your input.
I think I'm familiar with the Name Value pair; however, I'm not really sure on what explicit handles really are.
This is currently what I have for the bar graph.
x = [0 0.5 1];
y = [0 cels 0];
bar (x, y, 'r', 'LineWidth', 1)
axis([0 1 0 100]);
Rik
on 18 Nov 2018
Your current call to bar implicitly asks to create the bar plot in the current axes. You can make that explicit with something similar to the code below.
x = [0 0.5 1];
y = [0 cels 0];
bar (x, y, 'r', 'LineWidth', 1,'Parent',handles.axes1)
axis([0 1 0 100]);
Obadah M.
on 18 Nov 2018
Edited: Obadah M.
on 18 Nov 2018
Thank you for your input again, Rik.
Undefined variable "handles" or class "handles.axes1".
Error in temperature>convertTemp (line 176)
bar (x, y, 'r', 'LineWidth', 1, 'Parent', handles.axes1)
I'm not sure if I'm supposed to define the handle, or where to define it. Should I pass it in the convertTemp function?
Does the tag of the axes have to do with handle name?
Rik
on 18 Nov 2018
If you are using a GUI, you are probably sharing some data between callbacks. Typically that is stored and retrieved with the guidata function, with the objects as fields of that struct (with the fieldname equal to the object tag).
The main thing for your current question is that you need a reference to your axes object. I had assumed that you already had the handles struct loaded, with the axes being stored in handles.axes1. How you want to load the reference is up to you, but guidata is most of the time a good choice.
Rik
on 18 Nov 2018
The documentation mentions this, so you are reminded not to remove data that might already be there.
How guidata works is that you save a struct to your GUI figure with the line you quoted, and you can load that struct with the line below.
handles=guidata(gcbo);
(I prefer to use gcbo because that is accessible at any point in you callback stack, while hObject needs to be passed down as an input argument)
So if your target axes has the tag axes1, and you generated this function with GUIDE, the only thing you need to do is add the line loading handles to your bar code:
x = [0 0.5 1];
y = [0 cels 0];
handles=guidata(gcbo);
bar (x, y, 'r', 'LineWidth', 1,'Parent',handles.axes1)
axis([0 1 0 100]);
Rik
on 18 Nov 2018
You're welcome.
RE: your e-mail: an easy way to have two scales for a single bar plot is yyaxis. In that case it is especially important to use explicit handles to the axis you actually use for the bar plot, as the other is only for the human user.
Obadah M.
on 18 Nov 2018
Edited: Obadah M.
on 22 Nov 2018
Does this require 3 axes with the red bar in the middle? Or as you suggested, 1 axes and with the usage of yyaxis function. Which one would be simpler?
Because in the former, I can't get rid of the ticks in the bar graph, I've tried everything.
Thank you!!
Rik
on 18 Nov 2018
I can't see your image due to a 403 error, so I don't really understand your question.
(You can often check if someone else should be able to load a webpage like that by trying to open the page in incognito mode/private browsing mode/whatever your browser calls it)
Rik
on 19 Nov 2018
It might be better to create a patch object and modify its properties to adjust the height, but a bar plot should still work. (a patch would also allow you to create the black outer hull)
The yyaxis function will probably be the easiest way to have ticks on both sides.
Next time you can also use this editor to add in images.
Rik
on 20 Nov 2018
Your code is a bit confusing. Why are you using eval? Is that to convert a typed in value to a numeric value? You should use str2double for that. The eval function family should be avoided whenever possible. And what is LocalSetDisplay doing?
I would advise you to pick one temperature scale and use that for updating the chart. Split up functions whenever they perform two tasks: you can define C2F and F2C as anonymous functions and store them in the handles struct. Then you can use one function to ingest the text input, which then calls the plot updater and also sets the other text field.
I'll write up a small example.
Rik
on 20 Nov 2018
Below you will find a minimalist example that is fully self-contained and should be close to what you want. You might notice it doesn't use GUIDE with its convoluted functions.
function thermometer
handles=createGUI;
%create C2F and F2C
handles.C2F=@(C) (C * 9/5) + 32;
handles.F2C=@(F) (F - 32) * 5/9;
guidata(handles.fig,handles)
%set initial temp to 0C
set(handles.CButton,'String','0 C')
update(handles.CButton)
end
function h=createGUI
h=struct;
h.fig=figure;
%create thermometer axis
w=0.075;%width of axis
target_ax=axes('Parent',h.fig,'Position',[0.5-w/2 0.15 w 0.7]);
h.bar=bar(0.5,0,'BaseValue', -40,'Parent',target_ax);
axis(target_ax,[0 1 -40 100])
h.target_ax=target_ax;
yyaxis right
second_ax=gca;
h.second_ax=second_ax;
axis(second_ax,[0 1 -40 212])
set(target_ax,'XTick',[])
try
target_ax.Toolbar.Visible = 'off';
second_ax.Toolbar.Visible = 'off';
catch
end
%create text buttons
h.CButton=uicontrol('Parent',h.fig,...
'Style','edit',...
'Units','Normalized',...
'Position',[0.7 0.7 0.25 0.1],...
'String',' C',...
'Tag','C',...
'Callback',@update);
h.FButton=uicontrol('Parent',h.fig,...
'Style','edit',...
'Units','Normalized',...
'Position',[0.7 0.55 0.25 0.1],...
'String',' F',...
'Tag','F',...
'Callback',@update);
guidata(h.fig,h)
end
function update(hObject,eventdata)
%update bar plot and text of the other input field
%grab guidata
h=guidata(hObject);
%load text to value
val=str2val(hObject.String);
%---
%this is where you should check for NaNs, or empty inputs
%---
if strcmp(hObject.Tag,'C')
%update the bar plot without any need to convert to C
set(h.bar,'YData',val)
%update F text
set(h.FButton,'String',sprintf('%.1f F',h.C2F(val)))
else
%update bar plot (convert F to C first)
val=h.F2C(val);
set(h.bar,'YData',val)
set(h.CButton,'String',sprintf('%.1f C',val))
end
end
function val=str2val(str)
%extract the value from a char array
str( (str<48 | str>57) & str~=46)='';%remove non-number (keep .)
val=str2double(str);
end
Rik
on 21 Nov 2018
You already have attached your code as an m-file, so I don't understand why you would email it to me. If you have a question, it is best to post it here as either a comment or a new question so other contributors can also help you.
Rik
on 21 Nov 2018
OK, strange. The timestamp on the e-mail is about 4 hours ago, while your comment is 25 hours ago. So the e-mail function has some delay apparently.
Anyway, if you have any question, don't hesitate to post them here (if you create a new question, please post a link here as well).
Obadah M.
on 22 Nov 2018
Edited: Obadah M.
on 22 Nov 2018
Okay thank you Rik!
You have posted an improved version of the function I posted, however; I would still like to know what is causing this "bug". The whole area from 0 to -40 is red, is it another bar?
x = [0 0.5 1];
y = [0 cels 0];
bar (x, y, 'r', 'BaseValue', -40, 'LineWidth', 0.7)
set(gca, 'xtick', [], 'ytick', [])
axis([0 1 -40 120]);
Rik
on 22 Nov 2018
With the base value you indicate what should be the lower value of your bar plot. Then you choose to plot 3 bars, so you get 3 red stacks. I have already showed you what I would use as code, so I'm not going to look into big parts of your code.
More Answers (0)
See Also
Categories
Find more on Graphics Objects in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)