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 Kommentare

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
Thank you!it works!:)
and could you tell me how to rename the cells?
"...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;
hello
could you please precise how you want the cells to be renamed ?
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.
could you share some csv files to test my code ?
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]}
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
Stephen23
Stephen23 am 7 Apr. 2021
Bearbeitet: Stephen23 am 7 Apr. 2021
" 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.
Thank you Mathieu for your help! and also Thank you Stephen for your reflection!
Then, someone could tell me please how to read the csv in an array format and compile them for after applying a for loop?
I am currently saving each data in a cell of an array!
Thank you!
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 ?
Hi Mathieu!
My code is running with your feedback. Maybe, there are other options to make it more efficient, but at the moment does what I am expecting.
I am running several loops because I am doing a frequency analysis of my data using wavelets, and therefore I have several loops for the different types of handle the borders, mother wavelets and levels of decomposition.
Hello
glad it works
good luck for the future
Use the structure returned by DIR.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

"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.

Produkte

Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by