Quantcast
Channel: MATLAB Central Newsreader Recent Posts
Viewing all articles
Browse latest Browse all 19628

'index exceeds matrix dimensions' using text function with bar plot

$
0
0
I am trying to insert data values at the top of my bar plots using the text() function. I keep getting the error that 'index exceeds matrix dimensions'. I know what this error means, however my vectors in the text portion of the code all have the same length. An example of part of my plot code:

%% plot code

% cell data processed earlier that I convert into a 9x2 double (the data is numeric)
statplot_values = cell2mat(player_stats(2:end,[2 6]));

[r,c] = size(statplot_values);
x = [1:1:r];
y = statplot_values;
bar(x,y);

s1 = num2str(y(:,1)); % string generated for display for 1st column
s2 = num2str(y(:,2)); % 2nd column

ylim([0 100]);
text(x,y(:,1),s1,'horizontalalignment','center');
text(x,y(:,2),s2,'horizontalalignment','center');

I keep getting this error, however I am completely positive that my X vector is a 1x9 double, my Y matrix is 9x2 double, and my strings are both 9x2 characters.


I wrote extremely basic code before this to see if this format, and it does when I run:

x = [1 2 3 4 5];
y = [1 3; 5 7; 9 11; 13 15; 17 19];
bar(x,y);
s1 = num2str(y(:,1));
s2 = num2str(y(:,2));
text(x,y(:,1),s1,'horizontalalignment','center');
text(x,y(:,2),s2,'horizontalalignment','center');

I have no idea what is going on

Viewing all articles
Browse latest Browse all 19628

Trending Articles