Remove NaNs from struct fields embedded in a cell array
3 views (last 30 days)
Show older comments
Good afternoon,
I have a strange problem occuring when using import data has started adding random lines of NaNs, I do not know why this has happened but I need to remove them.
Within the attached sample, the struct is contained within a cell array, as the original code loops. WIthin the struct, the data field contains the 2D matrix, what I need this for any rows with NaN added to be removed from each one.
Can you help please?
Thanks
1 Comment
Stephen23
on 14 Jul 2024
"I have a strange problem occuring when using import data..."
Avoid using IMPORTDATA. Use a more reliable file importing tool (we could help you with that, if you upload a sample data file).
Answers (1)
dpb
on 14 Jul 2024
Edited: dpb
on 14 Jul 2024
"...when using import data"
That's the problem.
How about instead attaching a sample data file that creates the issue and let's solve the problem by not creating one in the first place....that would be the better solution.
But,
whos -file data_Struct_gau_sample.mat % what's in the file??
load data_Struct_gau_sample data_struct_Gau % load the variable of interest
g=data_struct_Gau{:}; % first dereference the cell, more convenient/shorter variable name
whos g % see what the cell contained
g1=g(1) % and finally, what the struct is...whew!!! a lot a work
for i=1:numel(g) % iterate over the struct array
ix=all(isfinite(g(i).data),2); % find rows in data row not containing Nan/Inf
g(i).data=g(i).data(ix,:); % keep only those
end
As alluded to in the intro remarks that I started as a comment and as @Stephen23 points out, IMPORTDATA() is a very unpredictable beast that acts like a chameleon -- it returns different things depending upon just what the file content is and you can't control it so you end up having to do stuff like the above...it's better to just avoid the grief in the first place. Frankly, while it is understandable what Mathworks was tyring to do to make MATLAB input essentially painless, in the end the attempted cure is worse than the illness.
0 Comments
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!