Using a Loop to read and store information?

조회 수: 2 (최근 30일)
Gabriela Garcia
Gabriela Garcia 2021년 1월 14일
댓글: Walter Roberson 2021년 1월 15일
I have data stored in 10 different excel documents. Each document has the same table-like format. My goal is to read the information I want to use from the excel data, load it into my script, and reorganize it into a new excel sheet. For this question, I want to know if there is a way that I can use a while or for loop to load my data, rather than type out load for each document.
Each excel document is titled in ascending order, such as 'a1', 'a2' ... 'a10'

채택된 답변

Walter Roberson
Walter Roberson 2021년 1월 14일
  댓글 수: 2
Gabriela Garcia
Gabriela Garcia 2021년 1월 15일
This helped me to load the data, thank you!
But I was not clear that I wanted this data in a table or matrix format so that I might compare them. I am relatively new to Matlab, so I am not sure if I can use this function to also read each excel as a matrix within the loop?
Walter Roberson
Walter Roberson 2021년 1월 15일
dinfo = dir('a*.xlsx');
filenames = {dinfo.name};
filenames = natsortfiles(filenames);
numfiles = length(filenames);
datacell = cell(numfiles,1);
for K = 1 : numfiles
datacell{K} = readmatrix(filenames{K});
end
Now you can examine datacell{1}, datacell{2} and so on.
This code does not assume that the matrices are all the same size.
In the special case that they are all the same size, then
data = cat(3, datacell{:});
would create a 3D matrix where the third dimension corresponds to different files.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by