フィルターのクリア

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 Center および File ExchangeLanguage Fundamentals についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by