Hello,
I have 20 excel sheet which contain a bunch of data. I want to extract element from these excel and make 2 matrix.
the first matrix is : row 32 from each excel sheet and only three elemet in coloum 6,8 and 12
the second matrix is row 33 from each excel sheet and only three element which are again element 6,8 and 12.
Is there an easy way to do it instead of importing each excel ark then hand piciking the elemet of each 20 excel ark and making them into a matrix ?

답변 (2개)

Dave B
Dave B 2021년 11월 16일
편집: Dave B 2021년 11월 16일

1 개 추천

When you call readtable (or readmatrix or readcell) you can specify a range. I think the range has to be contiguous (i.e. you could grab row 32 columns 6 to 12.
Having said that, it's probably easier to just read in the whole row. That's still less than reading in the whole thing (though you can check performance, my guess is it makes little difference):
a=readmatrix('foo.xlsx','Sheet','Sheet1','Range','F32:L32');
a([1 3 7])
ans = 1×3
57 67 87
% probably easier to read the whole row in:
b=readmatrix('foo.xlsx','Sheet','Sheet1','Range','32:32');
b([6 8 12])
ans = 1×3
57 67 87
% readtable version
c=readtable('foo.xlsx','Sheet','Sheet2','Range','33:33');
c(:,[6 8 12])
ans = 1×3 table
Var6 Var8 Var12 ____ ____ _____ 156 166 186

댓글 수: 7

I think you mean
a=readmatrix('foo.xlsx','Sheet','Sheet1','Range','F32:L33');
because they need to read BOTH row 32 and row 33. Then row 32 goes into matrix 1 and row 33 goes into matrix 2. And of course that is for just one workbook so you'll still need the loop over all workbook files.
Dave B
Dave B 2021년 11월 16일
You're totally right, I read it (incorrectly) as row 32 from sheet 1 and row 33 from sheet 2.
basma awad
basma awad 2021년 11월 19일
Is there a way to do a loop over 30 differenct excel sheet?
Sure! You can use sheetnames to get a list of excel sheets if you want all of the sheets in an excel file and they're not named in a way that you can construct the strings naturally (e.g. Sheet1, Sheet2, etc.)
sheets = sheetnames('MyExcelFile.xlsx')
for i = 1:numel(sheets)
a=readmatrix('foo.xlsx', 'Sheet', sheets(i), 'Range', 'F32:L33');
% ... do stuff with a here
end
basma awad
basma awad 2021년 11월 19일
Am sorry i have 20 different excel file, they are seperate and not in the same excel file. so 20 excel files.
For different files it's similar:
fp = 'C:\mypath\to\myexcelfiles';
% this is all files in a folder...if you have a different subset,
% or they're in different places, you'll need to come up with an
% alternate approach to telling MATLAB which files to read...
fl = dir(fullfile(fp,'*.xlsx'));
for i = 1:numel(fl)
a = readmatrix(fullfile(fp,fl(i).name), 'Range', 'F32:L33')
end
basma awad
basma awad 2021년 11월 19일
Thank you so much !!

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

Image Analyst
Image Analyst 2021년 11월 16일

0 개 추천

No "easier" way. Since you have 20 different workbooks with unique filenames, you're going to have to import each one one-at-a-time, and then extract the data and paste it into your two output matrices. See the FAQ for code samples to process a sequency of files.

카테고리

도움말 센터File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

질문:

2021년 11월 16일

댓글:

2021년 11월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by