I am using Bar plot for plotting the following code i am using
ABC=xlsread('ABC.xlsx');
figure(1);
for i= 0:3
A= bar(ABC(:,i+1:i+3))
set(gca,'XTicklabel',{'25','50','75','100'})
xlabel('time')
for j=1:3
text(A(i),sprintf('(%.0f)',A(i)))
end
end
But I want the y data on it how can i proceed any hint i am using text command...Any changes in code can anyone suggest Thank you in advance ..Also i am using Matlab 2016b version

 채택된 답변

Ankit
Ankit 2022년 1월 19일

0 개 추천

text(A(i),sprintf('(%.0f)',A(i))) - your syntax seems doesn't right. Please follow the following syntax to defined values of y on your bar plot.
text(x,y,'string','PropertyName',PropertyValue....).
I tested this code on 2014b. Though it can be easily done with later version.
close all;
x = [25 50 75];
vals = [6000 3000 3100;7000 3100 3200;7900 3100 3200];
b = bar(x,vals);
dx = [-5, 0 , 5];
for i = 1:length(x)
for j = 1:length(vals)
text(x(i)+dx(j),vals(i,j),num2str(vals(i,j),'%.0f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
end
end

댓글 수: 4

Prasad Joshi
Prasad Joshi 2022년 1월 19일
Thank you ankit it work for me ...But one error i am getting at the last part the data is not showing ..actually I am new to matlab coding can you please help me that also why this error I am attaching the code and graph
emissions=xlsread('emissions.xlsx');
i= 0
X=[ 25 50 75 100]
A= emission(:,i+1:i+3)
bar(X,A)
dx = [-5, 0 ,5];
for i = 1:length(x)
for j = 1:length(vals)
text(x(i)+dx(j),vals(i,j),num2str(vals(i,j),'%.0f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
end
end
Thank you ankit for the help ...Please can you guide in this also how can i solved it
Ankit
Ankit 2022년 1월 19일
As you are new to MATLAB please refer to the MATLAB onramp course. MATLAB Onramp - MATLAB & Simulink Tutorial (mathworks.com)
I would also recommend to refer to the MATLAB knowledge base. And following links are helpful too.
the outer loop runs 4 times for values from 25 to 100. The inner loop runs 3 times.
for j = 1:length(vals)
for i = 1:length(x)-1
text(x(j)+dx(i),vals(j,i),num2str(vals(j,i),'%.0f'),...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
end
end
Prasad Joshi
Prasad Joshi 2022년 1월 19일
Thank you ankit ...It worked
Prasad Joshi
Prasad Joshi 2022년 1월 19일
Ankit can you please help me out in this question also it would be highly helpful and thank you for the links I will go through it .
https://in.mathworks.com/matlabcentral/answers/1630970-for-loop-overriding-pervious-data?s_tid=srchtitle

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

KSSV
KSSV 2022년 1월 19일

1 개 추천

n = 5 ;
x = (1:n) ;
y = randi(n,n,1) ;
bar(x,y)
hold on
text(x,y+0.1,num2str(y))
ylim([0 6])
Simon Chan
Simon Chan 2022년 1월 19일
편집: Simon Chan 2022년 1월 19일

1 개 추천

Why not using the example in the documentation by getting the EndPoints as follows:
clear;
clc;
x = [25 50 75];
vals = [6000 3000 3100;7000 3100 3200;7900 3100 3200];
b = bar(x,vals);
dx = [-5, 0 , 5];
text(cat(2,b.XEndPoints),cat(2,b.YEndPoints),string(cat(2,b.YData)),'HorizontalAlignment','center',...
'VerticalAlignment','bottom')

댓글 수: 2

Ankit
Ankit 2022년 1월 19일
@Simon Chan: 'XEndPoints' property is available from 2019b version I think. And he is using 2016b.
Prasad Joshi
Prasad Joshi 2022년 1월 19일
Thank you Simon chan ...You answer was highly helpful

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

질문:

2022년 1월 19일

댓글:

2022년 1월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by