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

לק"י
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.

 Accepted Answer

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 Comments

Hi
if my contribution has helped you, do you mind accepting it ?
tx
לק"י
Hi mathieu, of corse, yes! i was away from the lab so I see it only now.
thanks!
לק"י
Hi mathieu, thanks again!
I tried the code, and was stuck at the natsortfiles.
An error appeard while running the natsortfiles line:
fileNames_sorted = natsortfiles({fileNames.name}); % sort file names into order (https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
The line was just copied and written well. the following error output was generated:
>> fileNames_sorted = natsortfiles({fileNames.name});
Undefined function or variable 'natsortfiles'.
I looked at the page you gave, but it seems that all is written well. maybe it is an old command or something like that.. can you help me find out what is wrong?
thanks ALOT again!
Amit.
לק"י
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!
My pleasure
yes I should have told you explicitely that there was a function from FEX to download
all the best

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!