Read multiple csv and plot

8 次查看(过去 30 天)
I have several csv files in a folder. I want to plot some graphes with each file.
This is my original code. It works, but I have to change file path every time.
csv_file = fopen('C:\Users\testing.csv','rt');
formatSpec = '%s%s%s%s%s%s%s%s%s%s%s';
C = textscan(csv_file, formatSpec, 'Delimiter', ';');
fclose(csv_file);
This the code that I manage to extract several files in a folder.
csv_dir = uigetdir(' ');
file_list = dir(fullfile(csv_dir, '*.csv'));
file_list_num = size(file_list);
file_list_names = {file_list.name};
csv_file = fopen('file_list','rt');
formatSpec = '%s%s%s%s%s%s%s%s%s%s%s';
C = textscan(csv_file, formatSpec, 'Delimiter', ';');
fclose(csv_file);
Error code:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Untitled2
C = textscan(csv_file, formatSpec, 'Delimiter', ';');

采纳的回答

dpb
dpb 2019-3-13
formatSpec = '%s%s%s%s%s%s%s%s%s%s%s';
csv_dir = uigetdir(' ');
d=dir(fullfile(csv_dir, '*.csv'));
for i=1:numel(file_list)
fid = fopen(fullfile(d(i).folder,d(i).name),'r');
C = textscan(csv_file, formatSpec, 'Delimiter', ';');
fid=fclose(fid);
...
DIR() returns a struct of directory info; have to dereference it...plus you have the text string 'file_list' in the argument, not the variable altho that alone wouldn't fix the problem.
See
doc dir
for full syntax, examples, etc., ...
  1 个评论
Ming-En Han
Ming-En Han 2019-3-14
Hello dpb
Thank you so much for you reply.
I really appreciate

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by