Hi
I'm trying to write a loop that reads a specific range of a worksheet each time, then for the next loop, the range shifts down 48 cells.
filename = xxx
sheet = 1
xlRange = D2:D49
f=xlsread(filename,sheet,xlRange)
the range then changes to D50:D97, then D98:D145 ...
I tried concatenate 2 strings to form the cell reference but it didn't work:
sRow = 2
lRow = sRow+47
sRowRef = strcat('D',num2str(sRow))
lRowRef=strcat('D',num2str(lRow))
xlRange='sRowRef:lRowRef'
Then I would just do sRow + 48
Unfortunately, xlsread didn't recognise this new xlRange. Is there another way to do it? Thanks

 채택된 답변

Amit
Amit 2014년 2월 1일
편집: Amit 2014년 2월 1일

0 개 추천

Your xlRange will produce string 'sRowRef:lRowRef' not 'D2:D49'. You should try:
xlRange = [sRowRef ':' lRowRef];

댓글 수: 3

Jeff
Jeff 2014년 2월 1일
I got an error saying "Error: A MATLAB string constant is not terminated properly."
Any ideas?
which line do you get the error from? did you used
xlRange = [sRowRef ':' lRowRef];
or otherwise you can use,
xlRange = strcat(sRowRef, ':', lRowRef) % IF this is more familiar to you
Jeff
Jeff 2014년 2월 1일
I got it. Forgot to put space in between. THANKS!!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 2월 1일

0 개 추천

Try this:
startingRow = 2;
for k = 1 : 10
cellRef = sprintf('D%d:D%d', startingRow, startingRow + 47);
fprintf('cellRef = %s\n', cellRef); % Show in command window.
startingRow = startingRow + 48;
end
In command window:
cellRef = D2:D49
cellRef = D50:D97
cellRef = D98:D145
cellRef = D146:D193
cellRef = D194:D241
cellRef = D242:D289
cellRef = D290:D337
cellRef = D338:D385
cellRef = D386:D433
cellRef = D434:D481

카테고리

도움말 센터File Exchange에서 Convert Image Type에 대해 자세히 알아보기

질문:

2014년 2월 1일

답변:

2014년 2월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by