MATLAB Answers


Determining the second last row

Asked by John
on 16 Mar 2012

Hi there,

How would you code the number of rows minus one? Below I use size() to find the number of rows, but I want to set nrows to the number or rows minus 1.

Many thanks

sch_cycle=xlsread('C:\Autonomie practice\cycle.xls','Input_data');
nrows = size(sch_cycle,1);




No products are associated with this question.

2 Answers

Answer by Jonathan Sullivan on 16 Mar 2012
 Accepted answer

sch_cycle=xlsread('C:\Autonomie practice\cycle.xls','Input_data');
nrows = size(sch_cycle,1)-1;


Answer by Dr. Seis
on 16 Mar 2012

If you mean you want to set "sch_cycle" to have nrows - 1, then no need to actually determine "nrows" beforehand (just use "end"):

sch_cycle = sch_cycle(1:end-1,:);

You can setup an "if" statement in order to make sure "nrows" is greater than 1.


or you could use max:


Dr. Seis
on 16 Mar 2012

Looks like "end" will still work there, too:

sch_cycle(1:max(1,end-1),:); % For all rows except last row

sch_cycle(max(1,end-1),:); % For just second to last row

on 16 Mar 2012

Thanks for all the suggestions guys.

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!