How to read dynamic range with xlsread function?

6 ビュー (過去 30 日間)
Suraj Srivastava
Suraj Srivastava 2015 年 2 月 20 日
コメント済み: Walter Roberson 2019 年 2 月 22 日
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.

採用された回答

Guillaume
Guillaume 2015 年 2 月 20 日
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 件のコメント
Russell Grm
Russell Grm 2019 年 2 月 21 日
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 2019 年 2 月 22 日
Personally I find using sprintf() to be simpler.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by