How to import several csv files (Nan,numeric,text) for operate with them after

24 ビュー (過去 30 日間)
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 件のコメント
Mathieu NOE
Mathieu NOE 2021 年 4 月 8 日
Hello
glad it works
good luck for the future
Stephen23
Stephen23 2021 年 4 月 8 日
Use the structure returned by DIR.

サインインしてコメントする。

採用された回答

Jeremy Hughes
Jeremy Hughes 2021 年 4 月 6 日
I suggest readtable.

その他の回答 (1 件)

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

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by