【发布时间】:2015-07-07 12:06:40
【问题描述】:
我正在尝试为输入数据绘制条形图并在每个条形上添加数据标签,当我运行程序时,我收到错误消息“未定义函数 'max' 用于类型为 'cell' 的输入参数。”我的代码是...
data = [3 6 2 ;9 5 1];
figure; %// Create new figure
h = bar(data); %// Create bar plot
%// Get the data for all the bars that were plotted
x = get(h,'XData');
y = get(h,'YData');
ygap = 0.1; %// Specify vertical gap between the bar and label
ylim([0 12])
ylimits = get(gca,'YLim');
%// The following two lines have minor tweaks from the original answer
set(gca,'YLim',[ylimits(1),ylimits(2)+0.2*max(y)]);
labels = cellstr(num2str(data')) %//'
for i = 1:length(x) %// Loop over each bar
xpos = x(i); %// Set x position for the text label
ypos = y(i) + ygap; %// Set y position, including gap
htext = text(xpos,ypos,labels{i}); %// Add text label
set(htext,'VerticalAlignment','bottom', 'HorizontalAlignment','center')
end
当我输入“data = [3 6 2 9 5 1]”时,程序运行良好
【问题讨论】:
标签: matlab