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