How to open csv files as separate numeric matrices from a certain directory?

I have many csv files in a folder and I need to open all as separate numeric matrices in workspace.

1 Comment

Don't make your code slow, complex, and inefficient with dynamically named variables:
The content of a cell array (as the accepted answer shows) are already separated arrays.

Sign in to comment.

 Accepted Answer

% csv_location = 'path\to\your\csv\files';
csv_location = '';
files = dir(fullfile(csv_location,'*.csv'));
for ii = 1:numel(files)
files(ii).data = readmatrix(fullfile(csv_location,files(ii).name));
end
Now the matrices are in the 'data' fields of the 'files' stucture array. You can access them like:
files(1).data
ans = 2×3
1 2 3 4 5 6
files(2).data
ans = 3×4
1 2 3 4 5 6 7 8 9 10 11 12
If you really want to make separate matrix variables in the workspace, you can do this instead:
files = dir(fullfile(csv_location,'*.csv'));
for ii = 1:numel(files)
assignin('base',sprintf('matrix_%d',ii),readmatrix(fullfile(csv_location,files(ii).name)));
end
whos('matrix_*')
Name Size Bytes Class Attributes matrix_1 2x3 48 double matrix_2 3x4 96 double

7 Comments

Thanks a lot, it worked.
But I need the matricies with their original file names not matrix_1, matrix_2 and so on
OK (this assumes that the file names are valid variable names - if not, you'll get an error):
% csv_location = 'path\to\your\csv\files';
csv_location = '';
files = dir(fullfile(csv_location,'*.csv'));
for ii = 1:numel(files)
[~,fn] = fileparts(files(ii).name);
assignin('base',fn,readmatrix(fullfile(csv_location,files(ii).name)));
end
whos()
Name Size Bytes Class Attributes a_file_containing_a_matrix 2x3 48 double another_file_containing_a_matrix 3x4 96 double ans 1x55 110 char csv_location 0x0 0 char files 2x1 1974 struct fn 1x32 64 char ii 1x1 8 double
"But I need the matricies with their original file names not matrix_1, matrix_2 and so on"
That is:
  1. impossible, because variable names cannot have period characters in them
  2. very bad data design, because you force yourself into writing slow, complex, inefficient code that is buggy and difficult to debug. Dynamically accessing variable names is not recommended.
  3. not required, because you can very efficiently access the imported file data and also the file meta-data (e.g. filenamdes) very simply by using very basic indexing, just as _ already showed you.
What _ showed you with
files(ii).data
to store the data is much simpler and more efficient than what you are trying to do.
@Stephen is referring to _(no name)'s first snippet, not the second snippet with assignin() and dynamically named variables. Again, see my answer and read the FAQ.
Thanks so much
Can I import the second column only from all files using this command?
readmatrix(path2mat,delimitedTextImportOptions('DataLines',[Inf,1]))
@Mustafa Sobhy readmatrix() has a 'Range' option where you can specify the second column.

Sign in to comment.

More Answers (1)

See the FAQ:
% Specify the folder where the files live.
myFolder = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures'; or wherever they are
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.csv'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an array with csvread() or readmatrix() or importdata().
data = imread(fullFileName);
end
By the way, you should process each set of data inside the loop. If you will need the data later, you can put it into a multidimensional array or a cell array.
It's a very bad idea to give each data set its own unique name. Why? See the FAQ:

Products

Release

R2021a

Tags

Asked:

on 13 Feb 2022

Edited:

on 25 Feb 2022

Community Treasure Hunt

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

Start Hunting!