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

15 visualizaciones (últimos 30 días)
Daniele Morello
Daniele Morello el 18 de Sept. de 2015
Comentada: Koen Baas el 27 de Jun. de 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')

Respuestas (1)

Nobel Mondal
Nobel Mondal el 18 de Sept. de 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 comentario
Koen Baas
Koen Baas el 27 de Jun. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Language Fundamentals en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by