0

I have a folder full of csv data files that I want to import to MATLAB. The import function will be the same for each of them. Their names follow a pattern, e.g. 'a0-b0.csv', 'a0-b5.csv', 'a0-b10.csv', 'a0-b15.csv' ... 'a2-b0' etc.

I want to import all these files at once. I want to import them as tables with sensible names that correspond to the data they contain.

Doing something like

filepath = 'file/path/'
for (a = [0, 2])
    for (b = [0:5:50])
        filename = strcat(filepath, 'a', num2str(a), '-b', num2str(b), '.csv')
        varname = strcat('a', num2str(a), '_b', num2str(b))
        varname = importfile(filename, startrow, endrow)
    end
end

made sense in concept to me.

As 'varname' itself is the variable, not the string it contains, this does not do what I want. I've seen a lot of answers to similar situations suggesting eval(), while simultaneously vehemently advocating against it.

eval() has side effects that make it annoying to implement. Everyone's intense aversion to it make me wonder if it's worth putting in the effort to try to implement it.

I can think of no other way to do this, however. I don't see how using arrays (the recommended alternative) would result in appropriate/relevant names.

Does anyone have a suggestion as to how to automatically import many csv files to tables in MATLAB with names that correspond to their filenames?


(Regarding duplicates: I haven't yet found a question that addresses the automatic creation of corresponding variable names, which is the main issue here)

userManyNumbers
  • 904
  • 2
  • 10
  • 26
  • 1
    use a `struct` to store the tables. do not use `eval`. ever. if you find yourself having to use it, time to rethink what you're doing. – transversality condition Apr 18 '16 at 01:42
  • More broadly, you want to use [dynamic field referencing](http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/) – sco1 Apr 18 '16 at 13:53

1 Answers1

0

As @transversality condition wrote in their comment, you should rethink you approach.

I'd suggest two steps:

  1. If you are about to change files in and import htem again, create .mat file with already imported data and check hwther to rescan the folder or load the .mat file.
  2. Decide whether you want to use struct class or cell class to contain your data.

The code for struct can be:

filepath = 'file/path/';                    % define the data folder
FileNames=dir('filepath\*.csv');            % list all csv. files within filepath
N=numel(FileNames);                         % count the csv files
DATA(N)=struct('name','','data',[]);        % preallocate DATA 
for (ii=1:N)
    Temp=regexp(FileNames(ii).name,'\.','split');  % separate file name and abbreviation
    DATA(ii).name = Temp{1};                % save the file name
    DATA(ii).data = importfile([filepath,'\',FileNames(ii).name], startrow, endrow);   % save the data
end

The code for cell can be:

filepath = 'file/path/';                    % define the data folder
FileNames=dir('filepath\*.csv');            % list all csv. files within filepath
N=numel(FileNames);                         % count the csv files
DATA=cell(2,N);                             % preallocate DATA
for (ii=1:N)
    Temp=regexp(FileNames(ii).name,'\.','split');  % separate file name and abbreviation
    DATA{1,ii} = Temp{1};                % save the file name
    DATA{2,ii} = importfile([filepath,'\',FileNames(ii).name], startrow, endrow);   % save the data
end

In both cases you have to find the appropriate line by, for example, DATA(find(strcmpi(DATA.name,'<name>))).data.

You can also use the cell approach to create struct. Suppose we've run the code for cell.

Then command

DataStruct=struct(DATA{:});

will allow you to access your data via Data.Struct.<filename> command directly.

Crowley
  • 2,279
  • 6
  • 22
  • 36