Remove NaNs from struct fields embedded in a cell array

3 Ansichten (letzte 30 Tage)
Richard Rees
Richard Rees am 14 Jul. 2024
Bearbeitet: dpb am 14 Jul. 2024
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 Kommentar
Stephen23
Stephen23 am 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).

Melden Sie sich an, um zu kommentieren.

Antworten (1)

dpb
dpb am 14 Jul. 2024
Bearbeitet: dpb am 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??
Name Size Bytes Class Attributes data_struct_Gau 1x1 5550862 cell
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
Name Size Bytes Class Attributes g 1x3 5550758 struct
g1=g(1) % and finally, what the struct is...whew!!! a lot a work
g1 = struct with fields:
data: [13596x17 double] textdata: {'GaussPoint,XTotalStress,YTotalStress,ZTotalStress,XEffectiveStress,YEffectiveStress,ZEffectiveStress,XYShearStress,XStrain,YStrain,ZStrain,XYShearStrain,DeviatoricStrain,PlasticState,XTotalStressIncrement,YTotalStressIncrement,ZTotalStressIncrement,StateParam1,StateParam2,StateParam3,StateParam4,StateParam5,StateParam6,StateParam7,StateParam8,StateParam9,StateParam10,StateParam11,StateParam12,StateParam13,StateParam14'}
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.

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by