Borrar filtros
Borrar filtros

Import several csv and modify the data

3 visualizaciones (últimos 30 días)
Gioele
Gioele el 15 de Dic. de 2021
Comentada: Gioele el 6 de En. de 2022
I need to import several csv files generated by a data analysis tool. The files have several columns and do not all have the same number of rows. After importing them I need to perform some calculations, also adding new columns. Then I have to select a part of the data, for example using logical indexing. For now I do this:
%Folder with all the csv data to read
Folder = 'MyFolder';
FileList = dir(fullfile(Folder, '**', '*.csv'));
% Import of all the data contained in thefolder
for iFile = 1:length(FileList)
thisFolder = FileList(iFile).folder;
thisFile = FileList(iFile).name;
File = fullfile(thisFolder, thisFile);
opts=detectImportOptions(File);
opts.VariableNamesLine = 2; % Row containing variables name
opts.DataLines = 6; % Number row to omit during data read, first data (t=0) will not be read)
opts.Delimiter =','; %Specifies that the data is comma seperated
Comp_tables{iFile} = readtable(File,opts, 'ReadVariableNames', true);
end
Now my problem is that the csv are saved in tables that are stored in a cell array, so for further processing I'm doing this:
for i = 1:length(Comp_tables)
t = Comp_tables{i}; % Extract table from cells array
t.Kraft = max(0, t.Kraft); % Substitution of negative value with zeros
t.Stress = max(0, t.Stress); % Substitution of negative value with zeros
Comp_tables{i} = t; % Save tables in cells array back
end
Is just an example, I would need to perform much more calculation. The problem is that I don't know which table which sample is since I can't change its name, I would like the table to have the same name as the csv file. Also I'm not sure if extracting files from cells array and then reinserting them afterwards is a good idea. It does make the job easier though since you can automate the calculations on all the tables.
Would there perhaps be a cleaner way to save the csvs, do some calculations, and finally have access to the data to be able to plot?
I have attached two files that I'm using.
  2 comentarios
Siddharth Bhutiya
Siddharth Bhutiya el 5 de En. de 2022
Since you want to do the same operations on all your tables, storing them in a cell array like this seems like the right thing to do. Unfortunately since the tables are not in the cell array, you cannot really name them. One way to preserve that information could be to store the name of the csv file in table's Description property. Although this information would not be visible when you display your table itself, you can still programatically access and check it by doing t.Properties.Description.
>> t.Properties.Description = 'myfile1.csv';
>> t.Properties
ans =
TableProperties with properties:
Description: 'myfile1.csv'
UserData: []
DimensionNames: {'Row' 'Variables'}
VariableNames: {'Var1' 'Var2' 'Var3'}
VariableDescriptions: {}
VariableUnits: {}
VariableContinuity: []
RowNames: {}
CustomProperties: No custom properties are set.
Use addprop and rmprop to modify CustomProperties.
Gioele
Gioele el 6 de En. de 2022
Thank you very much!

Iniciar sesión para comentar.

Respuestas (0)

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by