Effacer les filtres
Effacer les filtres

How to read dynamic range with xlsread function?

3 vues (au cours des 30 derniers jours)
Suraj Srivastava
Suraj Srivastava le 20 Fév 2015
Commenté : Walter Roberson le 22 Fév 2019
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.

Réponse acceptée

Guillaume
Guillaume le 20 Fév 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 commentaires
Russell Grm
Russell Grm le 21 Fév 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 le 22 Fév 2019
Personally I find using sprintf() to be simpler.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by