easy and efficient way to create cell array or table from csvs or existing variables

6 次查看(过去 30 天)
לק"י
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.

采纳的回答

Mathieu NOE
Mathieu NOE 2021-10-11
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 个评论
Amit Ifrach
Amit Ifrach 2021-10-13
לק"י
NVM, got it! I downloaded the function. sorry didn't know it's an external one and thought the link is explanation or something..
thanks!
Mathieu NOE
Mathieu NOE 2021-10-13
My pleasure
yes I should have told you explicitely that there was a function from FEX to download
all the best

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by