Removing nan values inside cells of a cell array and keeping the og structure

Hi,
I have looked into
and
However, as for my problem. I have a [1Xn] cell, where within it one of these is a [kXv] cell. It is easy to extract each of the cells in the cell array and remove all NAN, but I want to keep the origninal structure. Therefore I am asking if it is possible to have a simple line of code that doesent split up all of the cells in the cell array so I dont have to paste them toegether at a later stage?

2 Commenti

The solution depends on how you data is structured? Can you share your cell array?
Certain rows contain NAN, and I want to remove these rows, while still keeping this structure for a later part of my code.

Accedi per commentare.

Risposte (1)

Why would extracting those and removing nans change the original structure of the cell array? Why can't you just do this:
for k = 1 : numel(raw)
ca = raw{k}; % Get k'th cell of the raw cell array.
[rows, columns] = size(ca);
% ca is a cell array of character strings.
for col = 1 : columns
for row = 1 : rows
contents = ca{row, col};
% You cannot remove a cell from a cell array
% but you can set it's contents to null instead of NaN.
if isnan(contents)
contents = [];
end
end
end
end

4 Commenti

Hi IA.
Please may you look at this question:
https://www.mathworks.com/matlabcentral/answers/668383-recombine-cell-arrays-to-produce-a-new-image?s_tid=mlc_ans_email_ques
Some of the commands i tested put the values that were not NAN into one row. I employed your accepted answer in https://se.mathworks.com/matlabcentral/answers/318627-how-to-find-nan-values-in-a-cell-array, with a simple change to only work for the first row. This is the result:
Would giving the row a null number change the value of size, some entire columns are NAN and I only want to "replace"/remove ones that coincide with the first column. It seems that the code you recommended replaces every single NAN? Further:
to insert ca into raw, would
raw{k}=ca
suffice?
Yes. Since you forgot to include your variable, I had to do it without testing. I forgot that you have to stick contents back into ca, and then ca back into raw
ca{row, col} = contents; % Put repaired contents back into the cell.
raw{k} = ca; % Put the cell ca back into the cell array raw.
Attach your raw if you need more help.
save('answers.mat', 'raw');
Then use the paperclip icon to attach answers.mat.
Here is also a simple screenshot.
Since I at a later stage want to pair certain dates according to an equation and then remove them, according to something like this:
Salv={'2020-11-16 11:02:30'};
Constant=1;
for b=1:length(Salv)
for k=1:length(raw)
x = raw{1,k}(:,4);
rowNumber = strfind(x,Salv(Constant));
raw{1,k}(rowNumber,:) = [];
end
Constant=Constant+1;
end
It does not like that I have both cells and num together for the strfind.

Accedi per commentare.

Categorie

Richiesto:

il 2 Dic 2020

Commentato:

il 9 Dic 2020

Community Treasure Hunt

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

Start Hunting!

Translated by