Read multiple csv and plot

8 visualizzazioni (ultimi 30 giorni)
Ming-En Han
Ming-En Han il 13 Mar 2019
Commentato: Ming-En Han il 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', ';');

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by