필터 지우기
필터 지우기

How to select two (or more) different ranges using xlsread

조회 수: 13 (최근 30일)
Daniele Morello
Daniele Morello 2015년 9월 18일
댓글: Koen Baas 2022년 6월 27일
hi everyone, it's possible to select two or more different ranges using xlsread? for example i need to select columns from A to D ('A:D') and columns from H to M ('H:M')

답변 (1개)

Nobel Mondal
Nobel Mondal 2015년 9월 18일
You can patch the function, or write a new one to get the functionality:
Example:
[x,y,z] = xlsreadPatch('test.xlsx', 'Sheet1', {'A1:D10', 'H1:M10'});
Here is a sample function. You can write a similar one (with probably better error handling :) )
function [num, txt, raw] = xlsreadPatch(filename, sheetname, rangecell)
firstRange = rangecell{1};
[num, txt, raw] = xlsread(filename, sheetname, firstRange);
if length(rangecell) > 1
for k=2:length(rangecell)
[tNum, tTxt, tRaw] = xlsread(filename, sheetname, rangecell{2});
if isempty(num)
num = tNum;
else
num(1:size(tNum,1), end+1:end+size(tNum,2)) = tNum;
end
if isempty(txt)
txt = tTxt;
else
txt(1:size(tTxt,1), end+1:end+size(tTxt,2)) = tTxt;
end
raw(1:size(tRaw,1), end+1:end+size(tRaw,2)) = tRaw;
end
end
end
  댓글 수: 1
Koen Baas
Koen Baas 2022년 6월 27일
Hi! Nice function, thank you for sharing. Please change rangecell{2} into rangecell{k} if you want to read in more than 2 ranges. Now it will keep reading in the second range for the 3rd, 4th, etc. range.
Also, I changed "end+1:end+size(tNum,2)" into "k". Please find my modified code below.
function [num, txt, raw] = xlsreadPatch(filename, sheetname, rangecell)
firstRange = rangecell{1};
[num, txt, raw] = xlsread(filename, sheetname, firstRange);
if length(rangecell) > 1
for k=2:length(rangecell)
[tNum, tTxt, tRaw] = xlsread(filename, sheetname, rangecell{k});
if isempty(num)
num = tNum;
else
num(1:size(tNum,1),k) = tNum;
end
if isempty(txt)
txt = tTxt;
else
txt(1:size(tTxt,1),k) = tTxt;
end
raw(1:size(tRaw,1), k) = tRaw;
end
end
end

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

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by