简体   繁体   中英

Importing all CSV files from a directory into Datastore in MATLAB

I have 450 *.csv files in a directory and I want to collect/import all of them into one datastore for further processing. I have used the following code for collecting all the CSV files into one datastore.

       Path = 'Data/Dataset Collection/';
       Files = dir(Path);
       for k = 1 : length(Files)
          FileNames = Files(k).name;

          if (~strcmp(FileNames, '.'))
              if (~strcmp(FileNames, '..'))
                  ds = datastore([Path  FileNames], 'TreatAsMissing', 'NA');

                  if k == 3
                  ds_All = ds;
                  else
                  ds_All = [ds_All ds];
                  end
              end
          end

But, I am facing with this error:

Array formation and parentheses-style indexing with objects of class 'matlab.io.datastore.TabularTextDatastore' is not allowed. Use objects of class 'matlab.io.datastore.TabularTextDatastore' only as scalars or use a cell array.

I have two questions:

1- How can I use the better coding to only use one datesotre ( only ds ), NOT two ( ds and ds_All ).

2- If my solution is well enough, how can I overcome the error?

From the Matlab online help :

ds = datastore(location) creates a datastore from the collection of data specified by location. A datastore is a repository for collections of data that are too large to fit in memory. After creating ds, you can read and process the data.

So it seems you can just initialize your datastore by the folder where the files are in. Have you tried

Path = 'Data/Dataset Collection/';
ds = datastore(Path);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM