필터 지우기
필터 지우기

Creating a vector of tables

조회 수: 8 (최근 30일)
Calum Henderson
Calum Henderson 2020년 10월 7일
댓글: madhan ravi 2020년 10월 8일
I currently have a script that loops through a number of '.csv' files in a directory and creates a table with the data from each file:
directory = dir(dirname);
filenames = {directory(:).name}';
csvfiles = filenames(endsWith(filenames,'.csv'));
for i = 1:length(csvfiles)
file = [char(dirname) '/' char(csvfiles(i))];
CTLdata = readmatrix(file);
table = array2table(CTLdata,...
'VariableNames',{'Current','Voltage','Resistance'});
end
It reads the files correclty, but my issue is that only the final table is saved for use. I would like to produce a "vector of tables" of sorts, and I have tried:
tables(i) = table
among other things but I have had no luck.
What is the best way for me to save the info from each file in a separate table?
  댓글 수: 1
Stephen23
Stephen23 2020년 10월 7일
편집: Stephen23 2020년 10월 7일
Rather than getting all directory contents and filtering them afterwards, it is simpler to just get the required files in the first place:
S = dir(fullfile(dirname,'*.csv'));
csvfiles = {S.name};
You should be using fullfile instead of concatenating strings together (I fixed the cell indexing too):
file = fullfile(dirname,csvfiles{i});
And the answer to your qusetion is to use a cell array, just the the documentation recommends:

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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 10월 7일
Tables cannot be combined in a simple array. You need a cell array
directory = dir(dirname);
filenames = {directory(:).name}';
csvfiles = filenames(endsWith(filenames,'.csv'));
tables = cell(size(csvfiles))
for i = 1:length(csvfiles)
file = [char(dirname) '/' char(csvfiles(i))];
CTLdata = readmatrix(file);
table = array2table(CTLdata,...
'VariableNames',{'Current','Voltage','Resistance'});
tables{i} = table;
end
  댓글 수: 3
Ameer Hamza
Ameer Hamza 2020년 10월 8일
I am glad to be of help!
madhan ravi
madhan ravi 2020년 10월 8일
Naming a variable table will shadow the inbuilt function.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by