Clear Filters
Clear Filters

How to read dynamic range with xlsread function?

5 views (last 30 days)
I want to read an excel file through xlsread function of MATLAB, but I want to keep my range dynamic. it will depend on the value of a variable.
Kindly help me out.
Any suggestion is appreciated.
Thank you.

Accepted Answer

Guillaume on 20 Feb 2015
Just compute the range based on your variables,
startrow = 5; endrow = 100;
startcol = 11; endcol = 35;
data = xlsread('somefile', GetExcelRange(startrow, endrow, startcol, endcol);
function xlsrange = GetExcelRange(startrow, endrow, startcol, endcol)
xlsrange = sprintf('%s%d:%s%d', GetExcelColumn(startcol), startrow, GetExcelColumn(endcol), endrow);
function colstring = GetExcelColumn(colindex)
colstring = dec2base(colindex-1, 26);
digit = colstring < 'A';
colstring(digit) = colstring(digit) + 'A' - '1';
colstring(~digit) = colstring(~digit) + 9;
colstring(end) = colstring(end) + 1;
Russell Grm
Russell Grm on 21 Feb 2019
The simplest approach would be to concatenate your character array with the variable containing your range using a bracket. Here is an example:
YourVariable = 6; % The variable according to which the range of your data is determined
str_1 = 'A';
str_2 = num2str(YourVariable);
Str = strcat(str_1,str_2);
filename = 'filename';
sheet1 = 1;
data = xlsread(filename,sheet1,['1:',Str]);
Hope it helps.
Walter Roberson
Walter Roberson on 22 Feb 2019
Personally I find using sprintf() to be simpler.

Sign in to comment.

More Answers (0)


Find more on Get Started with MATLAB in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!