How can I write the content of the cells in a cell array to a text files

hello, given a herarical cell array as in the image below, how can I write the contetnt of the cells data{2,2}{i,1} to a seperate text file. i.e. instead of having them as cellarray>>table, Is there a way that can allow me to convert them to folder>>textfiles, and save them on my machine?

6 Comments

"Is there a way that can allow me to convert them to folder>>textfiles, and save them on my machine?"
Of course: use some loops (two looks like about the right number of loops).
What have you tried so far?
This is what I have tried so far but I am not getting any output
for i=1:size(DATA,2)% "DATA" is the cell array mentioned in question
currCell=DATA{i,2};% current cell in the iteration
name=['folder' num2str(i)]; %new folder name
mkdir(name) %create new folder
for j=1:size(currCell,1)
currTable=currCell{j,1};% current table in the iteration
filename=['file' num2str(i)];
writetable(currTable,filename);
end
end
filename=['file' num2str(i)];
% ^ you probably want j here, not i
Are any files created? Are you looking in the correct directory?
After the code has finished, what are the values of i and j ?
(tip: use ii and jj instead, as i and j are the imaginary unit).
I have edited my code and got the needed output but I am having an issue with naming the folders and the files as:
the folders should have the name of the text file that it origanlly came from, that path of these text file is already stored in the cell arra DATA{i,1} but I just want to get their names not the full path.
the text files inside a folder should hold the date value stored inside them as their names.
How I can possibly do that?
for ii=1:10% "DATA" is the cell array mentioned in question
currCell=DATA{ii,2};% current cell in the iteration
for jj=1:length(currCell)
name= %THE FOLDER NAME SHOULD HAVE THE NAME OF THE TEXT FILE IT ORIGANLLY CAME FROM; %new folder name
mkdir(name) %create new folder
currTable=currCell{jj,1};% current table in the iteration
filename= %THE FILE NAME SHOULD HAVE THE VALUE OF THE DATE THAT IS STORED IN THE TABLE "currTable"; % NEW TEXTFILE NAME
f = fullfile(name,filename);
writetable(currTable,f);
end
end
I am guessing you want something like:
[~,name] = fileparts(DATA{ii,1});
and this shoudl be in the ii, not in the jj loop:
for ii = 1:10 % "DATA" is the cell array mentioned in question
[~,name] = fileparts(DATA{ii,1});
mkdir(name)
currCell = DATA{ii,2}; % current cell in the iteration
for jj = 1:numel(currCell)
...
end
end
thank you very much. problem solved.

Sign in to comment.

Answers (0)

Categories

Asked:

MA
on 25 Jan 2022

Edited:

MA
on 27 Jan 2022

Community Treasure Hunt

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

Start Hunting!