Info

This question is closed. Reopen it to edit or answer.

Why reading in csv files will not work for me.

1 view (last 30 days)
Claire McEldowney
Claire McEldowney on 11 Mar 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
I currently have approximately 16 csv files in my directory, and using matlab I want to read them all in. I have used the following code:
for files = dir('*.csv')
Q = length(files);
for n=1:Q
% Read in CSV
data = csvread(files(n).name); % Read in the data
filename = files(n).name; % Get the file name
filename; % Print the file name
end
end
When I run this script it isn't reading in any files or printing them out. Does anyone know why?

Answers (1)

Rik
Rik on 11 Mar 2019
Why aren't you trying something like this?
files = dir('*.csv')
Q = numel(files);
data=cell(Q,1);
for n=1:Q
% Read in CSV
filename = files(n).name; % Get the file name
data{n} = csvread(filename); % Read in the data
fprintf('%s\n',filename); % Print the file name
end
And if this fails for some reason, report the error that is shown, or the size of the files variable.
  2 Comments
Claire McEldowney
Claire McEldowney on 11 Mar 2019
I Have run the script and in the command window it just shows the script name, rather than the names of the csv files?
Rik
Rik on 11 Mar 2019
Edited: Rik on 11 Mar 2019
Than your script must have the same name as the only csv file in your current folder. Put a breakpoint next to the first line and go through this code step by step to see what happens to your variables.
You may want to put this at the top of your code:
clc,clear variables;%for debugging only

Community Treasure Hunt

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

Start Hunting!