Using cellfun to remove nans from uniform cell

9 views (last 30 days)
Hi Guys, I have a bunch of cell values continaing 17 unifrom sized matrices that have NaN values in them. I need to replace these NaNs with any large place-holder number. I am trying to use a solution that has shown up in other posts:
https://www.mathworks.com/matlabcentral/answers/440856-replacing-nans-with-zero-in-a-matrix-within-a-cell-array
https://www.mathworks.com/matlabcentral/answers/459133-how-to-replace-nan-values-with-any-other-values-in-a-cell-array
Here is my code
RvarNnan1 = cellfun(@(M) subsasgn(M, substruct('()', {isnan(M)}), 123000), RvarNnan1, 'UniformOutput', 123000);
I am getting the following errors, prior to running:
"output must be assigned to a variable"
and after running get :
"Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false."
I think i don't understand the "M" variable. I've attached my cell variable(this is a sliced version {5x1}, of the actual variable {17x1} due to upload size restraits. Also of note, different from the other posts, I am replacing the NaNs with a meaningless large number. I have tried switiching around M with a predifenedvariable as well as using the actual VarNnan1.
Any help here is greatly appreciated!
  1 Comment
James Spalding
James Spalding on 19 Oct 2022
Resolved it myself. I needed to change the final field to 'false' and it ran correctly.
here's what the code looks like now.
RvarNnan1 = cellfun(@(M) subsasgn(M, substruct('()', {isnan(M)}), 123000), RvarNnan1, 'UniformOutput', false);

Sign in to comment.

Accepted Answer

dpb
dpb on 19 Oct 2022
If they are and always will be the same size, I'd use subterfuge...
[ra,ca]=size(R{1}); % save the array sizes first...
[r,c]=size(R); % and the cell array size
R=cell2mat(R);
R(inan(R))=0;
R=mat2cell(R,r*ones(ra,1),c*ones(ca,1)); % turn back to cell array
  3 Comments
James Spalding
James Spalding on 19 Oct 2022
Thanks for the feedback!
And yeah I would prefer to keep the nans but the next step for me is using Interp2 which can't have NaN values, but i need to maintain the dimensions of the arrays for the cordinates to match to the cordinates variables. I get around wildly wrong interpolated values using the 'nearest' option.
dpb
dpb on 19 Oct 2022
Edited: dpb on 19 Oct 2022
Oh. In that case I'd look at fillmissing instead...you could use one of the interpolation methods there instead and have "ready for prime time" data array for interp2. Or, if it is just using interp2 to fill the missing rather than actually interpolating to a finer grid, you've saved a step entirely.
Athough fillmissing only operates over one dimension; it can interpolate either by row or by column (default) but, won't/can't do a 2D interpolation over the surrounding values.

Sign in to comment.

More Answers (0)

Categories

Find more on Categorical Arrays in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!