Filter löschen
Filter löschen

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

13 Ansichten (letzte 30 Tage)
Daniele Morello
Daniele Morello am 18 Sep. 2015
Kommentiert: Koen Baas am 27 Jun. 2022
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')

Antworten (1)

Nobel Mondal
Nobel Mondal am 18 Sep. 2015
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 Kommentar
Koen Baas
Koen Baas am 27 Jun. 2022
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by