Importing multiple csv files as separate structures

조회 수: 4 (최근 30일)
Supriya Balaji Ramachandran
Supriya Balaji Ramachandran 2019년 7월 8일
답변: Dheeraj Singh 2019년 7월 16일
I have a folder with several subfolders, each subfolder has several csv files. How do I import each csv file as a structure and put all structures inside one large cell array
  댓글 수: 1
Stephen23
Stephen23 2019년 7월 9일
"How do I import each csv file as a structure and put all structures inside one large cell array "
Start by looking at the examples in the MATLAB documentation:

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

답변 (1개)

Dheeraj Singh
Dheeraj Singh 2019년 7월 16일
Hi,
I understand that you want to traverse folder which has subfolders, having csv files. You want to read csv files as a structure.
You can use dir to get the files and read files as tables and then convert the tables as structure.
The following code implements the above functionality.
%folder = path to the folder;
D=dir(fold)
%traverse from 3 to numel
for i=3:numel(D)
%subfolder
currD=D(i).folder;
%change directory
cd(currD);
fList=dir(D(i).name);
f=fList.folder;
for j=3:numel(fList)
pa=fullfile(fList(j).folder,fList(j).name);
%read filke as a table
content=readtable(pa);
%convert a table to a structure
content=table2struct(content)
end
%go back to the previous folder
cd(D(i).folder);
end

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by