이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
bar() to a specific axes in GUI
조회 수: 1 (최근 30일)
이전 댓글 표시
Obadah M.
2018년 11월 18일
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
채택된 답변
Rik
2018년 11월 18일
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
Obadah M.
2018년 11월 18일
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
2018년 11월 18일
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.
2018년 11월 18일
편집: Obadah M.
2018년 11월 18일
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
2018년 11월 18일
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.
Obadah M.
2018년 11월 18일
In the doc, it says that GUIDE already uses guidata.
I have this line at the OpeningFcn. What am I missing?
% Update handles structure
guidata(hObject, handles);
Rik
2018년 11월 18일
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]);
Obadah M.
2018년 11월 18일
편집: Obadah M.
2018년 11월 22일
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
2018년 11월 18일
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
2018년 11월 19일
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
2018년 11월 19일
Please attach an m-file with the code how you generated this. It is much easier to adapt code that almost work than to write new code. Another advantage is that it is easier to learn from edits as well.
Rik
2018년 11월 20일
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
2018년 11월 20일
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
2018년 11월 21일
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.
Obadah M.
2018년 11월 21일
I have emailed you the m-file before attaching the file, this was yesterday...
Rik
2018년 11월 21일
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.
2018년 11월 22일
편집: Obadah M.
2018년 11월 22일
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
2018년 11월 22일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
