MATLAB Answers

How I can modify this code to split the cells of a cell array?

83 views (last 30 days)
manar anwar
manar anwar on 19 Sep 2021 at 8:52
Edited: manar anwar on 26 Sep 2021 at 4:07
I am working on the following code, that it will go through some folder and read data files as tables,
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
t = readtable(fullFileName);
%output = t.output;
%fprintf(1, 'Now reading %s\n', fullFileName);
idxBkpt = find(diff([t.GDALT])<0);
split_indices = sort(1+idxBkpt); %beginnings of blocks
blk = diff([1 reshape(split_indices, 1, []) size(t,1)+1]);
splits = mat2cell(t, blk, size(t,2));
celldisp(splits);
end
Now what I am struggling with is the following:
I want to create a matrix that in the first cell of each row it will store the name of the data file i am processing and in the rest of the cells of that row it will store the cell array 'splits', any efficient way to do that?

Accepted Answer

Walter Roberson
Walter Roberson on 19 Sep 2021 at 9:17
nfiles = length(theFiles);
results = cell(nfiles,2);
fullnames = fullfile({theFiles.folder}, {theFiles.name});
results(:,1) = fullnames(:);
for k = 1 : nfiles
fullFileName = fullnames{k};
t = readtable(fullFileName);
idxBkpt = find(diff([t.GDALT])<0);
split_indices = sort(1+idxBkpt); %beginnings of blocks
blk = diff([1 reshape(split_indices, 1, []) size(t,1)+1]);
splits = mat2cell(t, blk, size(t,2));
results{k,2} = splits;
end
So results will be a cell array that is nfiles x 2. results{k,1} will be filename #k. results{k,2} will be the cell array splits -- and you will need to index that cell array to get to the pieces.
You cannot just use a cell array with N + 1 columns because it appears that the number of splits for each file may be different.
  9 Comments
manar anwar
manar anwar on 20 Sep 2021 at 6:09
Thank you very very much for all the help, it worked perfectly now.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!