Delete Cells in Cell array in two loops....

Hi everyone,
I have the following code and want to delete the empty cells in the results:
tt1 = find(year(Time) == 2015 & month(Time) == 10);
tt2 = find(year(Time) == 2010 & month(Time) == 10);
tt3 = find(year(Time) == 2019 & month(Time) == 04);
JT = [ tt1; tt2; tt3 ];
H= [ 4, 8];
for h = H
for jt = JT'
PdfDat{h,jt+h}=strcat('T',num2str(year(Time(jt+h))), 'Q', num2str(ceil(month(Time(jt+h)) / 3)));
Pdfd{h,jt+h}=ResMatch_LU.PST(jt+h, :);
end
end
The results are the following in which I want to delete the [ ] such that the array will be presented in fucntion of 'h' and 'jt+h' :
I want also to delete [ ] in the cell "1x401 double" here in coulumns 52 and 56;
Could you please help me to do those changes? My Matlab version is R2015a.
Many thanks!

回答(1 个)

You can use cellfun: Apply a function to each cell of a cell array
'isempty' -- true for cells containing an empty array, false otherwise
PdfDat_new = PdfDat(~cellfun('isempty',PdfDat));
Pdfd_new = Pdfd(~cellfun('isempty',Pdfd));

8 个评论

Thank you Ankit
Your code lines provide the below results, wile I want that those results be presented in function of "h". In other words, I need to have 2 cells of (3x1 cell) for each "H" instead of the below 6x1 cells. How to do that?
I want that for each H , we have :
You can achieve that by reshape.
PdfDat_new = reshape(PdfDat(~cellfun('isempty',PdfDat)),2,[])';
PdfDat_new1 = out(:,1); % 3x1 cell
PdfDat_new2 = out(:,2); % 3x1 cell
Thank you Ankit,
I got the following error with reshape:
Also, I don't have the "out ()" function with my R2015a version.
When I correctly use "Reshape", PdfDat_new = reshape(PdfDat(~cellfun('isempty',PdfDat)),[],1)';
I am getting the following cells which should not be split uniformly in two cells, rather I need to have here
PdfDat_new1 = [PdfDat_new(1,1), PdfDat_new(1,3), PdfDat_new(1,5)];
PdfDat_new2 = [PdfDat_new(1,2), PdfDat_new(1,4), PdfDat_new(1,6)];
These cells are in function of "H".
How can I do that? And how can I overcome the issue with the function "Out"?
Many thanks again
PdfDat_new1 = PdfDat_newout(1:2:5,:);
PdfDat_new2 = PdfDat_newout(2:2:6,:);
You can also use for loop and other ways to achieve as per your requirement
You should avoid numbered variables. It sounds like the number of PdfDat_new variables can change. If you can't guarantee that there will only be exactly 2, you should consider putting them in a cell array so you can index them in a loop.
Hi Rik,
Yu are right. I should avoid numbering variables as I know that the cells coming from H will not be exactly 2.
I am still looking for the solution, but i am stuck.
Would anyone help me?
Thank you.
why dont you use the method mentioned by rik? here with limited info and without having your relevant file it is not easy to reproduce the same problem
It would probably help if you replace this loop:
for h=H
with this loop:
for n_H=1:numel(H)
h=H(n);
...
PdfDat_new{n_H}=...

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2015a

标签

提问:

2022-1-31

评论:

Rik
2022-2-1

Community Treasure Hunt

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

Start Hunting!

Translated by