필터 지우기
필터 지우기

How to read dynamic range with xlsread function?

조회 수: 3 (최근 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 CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by