How to read dynamic range with xlsread function?

7 visualizzazioni (ultimi 30 giorni)
Hi,
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.

Risposta accettata

Guillaume
Guillaume il 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);
With:
function xlsrange = GetExcelRange(startrow, endrow, startcol, endcol)
xlsrange = sprintf('%s%d:%s%d', GetExcelColumn(startcol), startrow, GetExcelColumn(endcol), endrow);
end
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;
end
  2 Commenti
Russell Grm
Russell Grm il 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.
Cheers,
Walter Roberson
Walter Roberson il 22 Feb 2019
Personally I find using sprintf() to be simpler.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by