Read multiple csv and plot

4 views (last 30 days)
Ming-En Han
Ming-En Han on 13 Mar 2019
Commented: Ming-En Han on 14 Mar 2019
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', ';');

Accepted Answer

dpb
dpb on 13 Mar 2019
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 Comment
Ming-En Han
Ming-En Han on 14 Mar 2019
Hello dpb
Thank you so much for you reply.
I really appreciate

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!