easy and efficient way to create cell array or table from csvs or existing variables
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Amit Ifrach
el 11 de Oct. de 2021
Comentada: Mathieu NOE
el 13 de Oct. de 2021
לק"י
Hello!
I want to know if there an easy way (may be GUI) or other command to creat one table or cell array from several CSV files or several variables.
Here is an example:
I got several CSV files which in each one I need only 2 columns (lets asume colums C and D):
I want an easy way to creat one cell array from several csvs such as the above.
Furthermore, the extracted vectors above should be as it is (2d vector) in specific cell just for it. for each 2d vector new row (cell) should be created in the cell array, such as this:
Further more, lets assume I have several var's loaded to the workspace:
is there an easy fast way to combine several of these into a table in the same fashion as mentioned above?
thank you very very much!
Amit.
0 comentarios
Respuesta aceptada
Mathieu NOE
el 11 de Oct. de 2021
hello
this is my 2 cents suggestion
clc
clearvars
%% first section : CSV file management
fileDir = pwd;
% Sorting filenames in natural order
fileNames = dir(fullfile(fileDir,'*.csv')); % get list of data files in directory
fileNames_sorted = natsortfiles({fileNames.name}); % sort file names into order (https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
M = numel(fileNames_sorted);
% Import each file using it's filename
for k = 1:M
tmp = readcell(fullfile(fileDir, fileNames_sorted{k}));
% select columns
cols = (3:4);
name1{k,1} = ['2d vector from CSV ' num2str(k)];
data1{k,1} = tmp(:,cols);
end
% export as table or cell array
out_table1 = table(name1,data1);
out_cell1 = table2cell(out_table1);
%% second section : save workspace data
S = who();
for k = 1:numel(S)
name2{k,1} = S{k};
data2{k,1} = eval(char(name2{k,1})); % eval not recommended even if it works here
end
% export as table or cell array
out_table2 = table(name2,data2);
out_cell2 = table2cell(out_table2);
5 comentarios
Mathieu NOE
el 13 de Oct. de 2021
My pleasure
yes I should have told you explicitely that there was a function from FEX to download
all the best
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!