How to import several csv files (Nan,numeric,text) for operate with them after
古いコメントを表示
I want to import several csv files in matlab (each one for one streamflow station) where I have seasonal flows.This the structure of the each csv is the following:

I am trying to do in several ways but without success:
This the first way I tried:
% Get an array of all files
basepath = 'C:\Users\salonsov\Desktop\Results_lowflows\seasons';
files = dir(fullfile(basepath, '*.csv'));
% Pre-allocate data storage
data = cell(size(files));
% Import each file using it's filename
for k = 1:numel(files)
data{k} = csvimport(fullfile(basepath, files(k).name));
end
My I go the error that Unrecognized function or variable 'csvimport'.
I have also tried using csvread instead of csvimport, but I have an error because my data has NaN values.
I have tried also with read.table, but I cannot save the files in a cell.
After importing the csv files, I want to loop over them to perform some operations.
What is it the best way to import csv files and after operate with them in matlab?
16 件のコメント
David Fletcher
2021 年 4 月 6 日
have a look at readmatrix, it might do what you want
Mathieu NOE
2021 年 4 月 6 日
hello
either try readcell (like below) or combine readtable with table2cell
% Import each file using it's filename
for k = 1:numel(files)
data{k} = readcell(fullfile(basepath, files(k).name));
end
Sara Alonso Vicario
2021 年 4 月 6 日
Stephen23
2021 年 4 月 6 日
"...could you tell me how to rename the cells?"
Individual cells of a cell array do not have names, so they cannot be "renamed".
The entire cell array can be "renamed" by allocating it to a new variable:
A = B;
Mathieu NOE
2021 年 4 月 6 日
hello
could you please precise how you want the cells to be renamed ?
Sara Alonso Vicario
2021 年 4 月 6 日
Mathieu NOE
2021 年 4 月 6 日
could you share some csv files to test my code ?
Sara Alonso Vicario
2021 年 4 月 6 日
Mathieu NOE
2021 年 4 月 7 日
ok
so there is no "name" inside the file itself , I took the file name as what you wanted to have as name.
my first code suggestion is to create a structure :
files = dir(fullfile(basepath, '*.csv'));
% Pre-allocate data storage
data = cell(size(files));
% Import each file using it's filename
for k = 1:numel(files)
filename = files(k).name;
data{k}.name = filename(1:length(filename)-4);
data{k}.data = readcell(fullfile(basepath, filename));
end
% so the names are accessible like this :
>> data{1}.name
ans =
'S09489499'
>> data{2}.name
ans =
'S09489500'
% and of course you have access to the data like this
data{1}.data
ans =
145×3 cell array
{'year'} {'season'} {'mean.value'}
{[1953]} {[ 1]} {'NA' }
{[1953]} {[ 2]} {'NA' }
{[1953]} {[ 3]} {'NA' }
{[1953]} {[ 4]} {[ 29.9344]}
{[1954]} {[ 1]} {[ 30.7667]}
Mathieu NOE
2021 年 4 月 7 日
so this is another option to create one cell array per file, and the cell array name is the file name
files = dir(fullfile(basepath, '*.csv'));
% Pre-allocate data storage
data = cell(size(files));
% Import each file using it's filename
for k = 1:numel(files)
filename = files(k).name;
name = filename(1:length(filename)-4);
data = readcell(fullfile(basepath, filename));
eval([name,'=data;']);
end
here my workspace :
Name Size Bytes Class Attributes
S09489499 145x3 48720 cell
S09489500 273x3 91728 cell
" this is another option to create one cell array per file, and the cell array name is the file name"
An option which makes accessing the data more complex, inefficient, and latent buggy.
The filename is (meta-)data, and data should be stored in an array.
Sara Alonso Vicario
2021 年 4 月 7 日
Mathieu NOE
2021 年 4 月 7 日
I knew I would get comments from the second option - was waiting for it ! it didn't take long !!!
@ Sara
maybe you should more precisely explain what the next for loop is supposed to do , because there is maybe no need to create a second for loop
why not try to do all the work in one loop ?
Sara Alonso Vicario
2021 年 4 月 7 日
Mathieu NOE
2021 年 4 月 8 日
Hello
glad it works
good luck for the future
Stephen23
2021 年 4 月 8 日
Use the structure returned by DIR.
採用された回答
その他の回答 (1 件)
Stephen23
2021 年 4 月 8 日
"I want the cells to be rename because I want to know to which station corresponds the data in each cell. As you can see I could import the data, but now I am not sure how to know at which station corresponds each cell data."
A simpler, neater, more efficient solution is to use the structure returned by DIR:
P = 'C:\Users\salonsov\Desktop\Results_lowflows\seasons';
S = dir(fullfile(P, '*.csv'));
for k = 1:numel(S)
F = fullfile(P, S(k).name);
S(k).data = csvimport(F); % or READTABLE or whatever.
end
Take a look in the structure S: it contains all of your file data and the corresponding filenames, just as you require.
For example, the 2nd filename and its data:
S(2).name
S(2).data
Accessing and processing this will be much simpler than messing around with dynamically named variables.
カテゴリ
ヘルプ センター および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
