MATLAB Answers

Unable to perform assignment because the left side is 1-by-3 and the right side is 1-by-2

5 views (last 30 days)
Hello!
I am still struggling with trying to make graphs with categorical variables and error bars.
I am getting an error in line 11 (starts with ctr) that says "Unable to perform assignment because the left side is 1-by-3 and the right side is 1-by-2".
Am I missing a blatant 1-by-3 vector and how do I fix it without ruining my code? I apprieciate any help I can get, as I started learning Matlab just this year. Thanks!!
Here is my current code:
xc = categorical({'PTSD-','PTSD+'});
x = 1:numel(xc);
y = [.09 .13 ; -.08 -.35];
hBar = bar(x,y);
pos = [.1 .15 .2 .21];
neg = pos
posr = reshape(pos, 2, [2]);
negr = reshape(neg, 2, [2]);
for k1 = 1:size(y,2)
ctr(k1,:) = bsxfun(@plus, 1:numel(hBar(k1).XData), hBar(k1).XOffset');
ydt(k1,:) = hBar(k1).YData;
end
set(gca, 'XTick',x, 'XTickLabel',xc,'FontSize',12, 'FontName', 'Helvetica')
ylabel("Overall Cognitive Functioning (Z-Score)", 'FontSize',12, 'FontName', 'Helvetica')
ylim([-.5 .5]);
yticks([-.5, -.4 -.3 -.2 -.1 0 .1 .2 .3 .4 .5])
legend('location','northeast')
legend('FontName', 'Helvetica', 'FontSize',12)
legend boxoff
box off
legend boxoff
newcolors = ['#CBCBCB'; '#0033A0']
colororder(newcolors)
legend({'Met-','Met+'})
set(gca, 'XTick',x, 'XTickLabel',xc,'FontSize',12, 'FontName', 'Helvetica')

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 21 Oct 2020
I’m not certain what you’re doing with that line.
The standard way of coding it (to put errorbar objects or text objects at the top of the bars) is:
ctr(k1,:) = bsxfun(@plus, hBar(k1).XData, hBar(k1).XOffset')
I suspect that diference could be the problem, however I find it a bit difficult to follow your code, because I don’t see that you use ‘ctr’ or ‘ydt’ anywhere else.

  4 Comments

Show 1 older comment
Star Strider
Star Strider on 21 Oct 2020
You may have used the same block of code earlier, and set ‘ctr’ to a different size. The only fix for that I can think of is to re-define ‘ctr’ and ’ydt’ before the loop. The only other problem I can see is that the loop should be defined by ‘size(y,1)’ to make it appropriatlely general (although with a square matrix, that problem would not be evident).
Try this:
xc = categorical({'PTSD-','PTSD+'});
x = 1:numel(xc);
x = 1:2;
y = [.09 .13 ; -.08 -.35];
hBar = bar(x,y);
pos = [.1 .15 .2 .21];
neg = pos;
posr = reshape(pos, 2, [2]);
negr = reshape(neg, 2, [2]);
ctr = zeros(size(y)); % <— ADD THIS PREALLOCATION
ydt = zeros(size(y)); % <— ADD THIS PREALLOCATION
for k1 = 1:size(y,1)
ctr(k1,:) = bsxfun(@plus, hBar(k1).XData, hBar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = hBar(k1).YData; % Individual Bar Heights
end
If my diagnosis is correct, the preallocations should be the cure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by