MATLAB Answers


Index exceeds matrix dimensions.

Asked by Light
on 8 Jun 2013
f = figure('Position',[10 10 600 600]);
dat = {-1,1,1;0,-1,0;0,0,-1;1,0,0};
cnames = {'1','2','5'};
rnames = {'1','2','3','4'};
t = uitable('Parent',f,'Data',dat,'ColumnName',cnames,... 
          'RowName',rnames,'Position',[10 10 590 590]);
      blnA = logical( A == -1 );
blnOut = find(any(A == -1,2));
negcolumn = find(A(min(blnOut),:) == 1);
Error message occurred. I couldn't find the mistake.(cnames{max(negcolumn)})=5 So it must be dU(5)=7 but error message

Index exceeds matrix dimensions.

  1 Comment

I cannot run your code, because I do not have the array A. Is it possible for you to post self-contained code that will execute and show that error?



No products are associated with this question.

2 Answers

Answer by Image Analyst
on 8 Jun 2013
 Accepted answer


A = cell2mat(dat);

but I can't figure out what dU is for - it's not being used. Anyway, look at this code:

maxValue = max(negcolumn)
index = cnames{maxValue} % Gives string '5'
dU(5) % Works fine.
dU(cnames{max(negcolumn)}) % dU('5') = du(53) which is not defined because dU has only 5 elements.

You can't have the string '5' be the index into an array. Perhaps you wanted cnames to be a cell array that stored integers, not strings:

cnames = {1,2,5};

which works, though I still don't know what you're after.


Answer by Walter Roberson
on 8 Jun 2013

You have not defined dU()

cnames indexed by a number gives you a string. Indexing an array by a string is not impossible but seldom gives the expected answer: in your case it would be equivalent to asking for dU([49 50])

You need to remember that '1','2','5' are strings and that you almost never index arrays at strings; arrays get indexed at row numbers and column numbers.

If you need to be able to access an array by the name of a row, or the name of a column, then you need to use the MATLAB database object, which is part of the Statistics toolbox.


on 8 Jun 2013

I was wrong. It is not working

Here, index is the string '35', not the numeric value 35.

See my answer to your new explanation in your duplicate question:

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!