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

조회 수: 10 (최근 30일)
MA
MA 2021년 9월 19일
댓글: Walter Roberson 2022년 10월 6일
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?

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 19일
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.
  댓글 수: 11
MA
MA 2022년 10월 6일
hello again @Walter Roberson,
I have been trying to use the code you have provided previosly on another data and i am getting the following error, would you mind please if you can help me in figuring out how to fix the follwing error..
Unable to perform assignment because the size of the left side is 8-by-23 and the size of the right side is 22-by-1.
Error in Ionosonde_DatA_2 (line 88)
packed(:,1:numsplits(K)+1) = DATA{K,1};
and this is the code i am implementing
t1 = JRO(JRO.YEAR == Years(1), :);
doy=unique(tt.DayOfYear);
DATA = cell(length(doy),1);
for n = 1 : length(doy)
t2=t1(t1.DayOfYear == doy(n), :);
idxBkpt = find(diff([t2.GDALT])<0);
split_indices = sort(1+idxBkpt); %beginnings of blocks
blk = diff([1 reshape(split_indices, 1, []) size(t2,1)+1]);
splits = mat2cell(t2, blk, size(t2,2));
DATA{n,1} = splits;
end
numsplits = cellfun(@numel, DATA(:,1));
maxsplits = max(numsplits);
packed = cell(length(doy), 1+maxsplits);
for K = 1 : length(doy)
packed(:,1:numsplits(K)+1) = DATA{K,1};
end
the table t1 is attached
thank you so much in advance.
Walter Roberson
Walter Roberson 2022년 10월 6일
t1 = JRO(JRO.YEAR == Years(1), :);
doy = unique(t1.DayOfYear); %changed
DATA = cell(length(doy),1);
for n = 1 : length(doy)
t2=t1(t1.DayOfYear == doy(n), :);
idxBkpt = find(diff([t2.GDALT])<0);
split_indices = sort(1+idxBkpt); %beginnings of blocks
blk = diff([1 reshape(split_indices, 1, []) size(t2,1)+1]);
splits = mat2cell(t2, blk, size(t2,2));
DATA{n,1} = splits;
end
numsplits = cellfun(@numel, DATA(:,1));
maxsplits = max(numsplits);
packed = cell(length(doy), 1+maxsplits);
for K = 1 : length(doy)
packed(K,2:numsplits(K)+1) = DATA{K,1}; %changed
end
and remember to put the file names into packed(:,1)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by