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

12 vues (au cours des 30 derniers jours)
Daniele Morello
Daniele Morello le 18 Sep 2015
Commenté : Koen Baas le 27 Juin 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')

Réponses (1)

Nobel Mondal
Nobel Mondal le 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 commentaire
Koen Baas
Koen Baas le 27 Juin 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Language Fundamentals dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by