Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Why reading in csv files will not work for me.

1 回表示 (過去 30 日間)
Claire McEldowney
Claire McEldowney 2019 年 3 月 11 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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?

回答 (1 件)

Rik
Rik 2019 年 3 月 11 日
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 件のコメント
Claire McEldowney
Claire McEldowney 2019 年 3 月 11 日
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 2019 年 3 月 11 日
編集済み: Rik 2019 年 3 月 11 日
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!

Translated by