Error: "Subscripted assignment dimension mismatch."

조회 수: 1 (최근 30일)
sophp
sophp 2020년 4월 24일
답변: Athul Prakash 2020년 4월 30일
After running this in MATLAB, the correct bar graph is produced but without the error bars. The warning: "Subscripted assignment dimension mismatch." is returned, however I do not understand how.
x=categorical({'Q3','Q6','Q10','Q15','Q30','Q50'});
z=[2.891586318,1.99422657,1.907652812,4.71298388,6.007681496,6.718181667;3.229392123,3.36482536,4.089746011,5.204662107,6.761377397,7.978099976];
y=transpose(z);
a=[0.167797,0.314574,0.081018,0,0.116596,0;0,0.173132,0,0.231752,1.15677,1.204734];
errorplus=a;
errorminus=errorplus;
figure;
bar(x,y);
hBar = bar(y, 0.8);
for k1 = 1:size(y,2)
ctr(k1,:) = bsxfun(@plus, hBar(k1).XData, hBar(k1).XOffset');
ydt(k1,:) = hBar(k1).YData;
end
hold on
errorbar(ctr, ydt, errorplus, '.r')
hold off
ylabel('Signal Intensity','FontSize',18)
set(gca,'linewidth',2,'FontSize',14)
ylim([0 10]);
set(gca,'XTickLabel',x)
  댓글 수: 1
Tommy
Tommy 2020년 4월 24일
I get a bar graph with error bars and no error. Do you happen to have variables ctr and/or ydt stored in your workspace with sizes other than 2x6? Using clear before running would, I think, solve your problem. Preallocating ctr and ydt would be better, i.e.
ctr = zeros(2,numel(x));
ydt = zeros(2,numel(x));
for k1 = 1:size(y,2)
ctr(k1,:) = bsxfun(@plus, hBar(k1).XData, hBar(k1).XOffset');
ydt(k1,:) = hBar(k1).YData;
end

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

답변 (1개)

Athul Prakash
Athul Prakash 2020년 4월 30일
I think you need to clear all variables and close all figures before running this code.
Your code is working fine on my MATLAB R2019b and gives the proper bar graph.
If that doesn't solve your issue, please add the error message to your question (include a screenshot) so that we can see exactly what/where the error is. Could you also comment on your MATLAB version?

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by