Read multiple csv and plot

8 vues (au cours des 30 derniers jours)
Ming-En Han
Ming-En Han le 13 Mar 2019
Commenté : Ming-En Han le 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', ';');

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by