Delete empty field from struct to allow polyfit to work

1 vue (au cours des 30 derniers jours)
mashtine
mashtine le 26 Août 2014
Commenté : mashtine le 26 Août 2014
Hey,
I have a struct that is structured as structname.heights.bin01 (bin02, bin03 etc). I would like to run polyfit function on the bins (binned wind speed) but some bins are empty ([]) and this kills polyfit and ends the loop. Ideally I would like to remove the empty fields in the struct and then run polyfit
HOWEVER with the script I have, I am afraid that if I were to delete bin45 for instance because it were empty, the loop will cancel when searching for bin45 as it does not exist and would not continue to bin46.
Here is the script so far:
inpdata = Tower_bins_wind_GF_allyrs;
heights = fieldnames(inpdata);
bins = fieldnames(inpdata.height10m);
names = strtrim(cellstr(num2str([1:length(bins)]'))');
names = strrep(strcat('params',names),'.','');
params = cell(numel(bins),1);
for h = 1:numel(heights);
for i = 1:numel(bins);
inpdata.(heights{h}).(bins{i})(:,3) = log(inpdata.(heights{h}).(bins{i})(:,1));
inpdata.(heights{h}).(bins{i})(:,4) = log(inpdata.(heights{h}).(bins{i})(:,2));
inpdata.(heights{h}).(bins{i}) = inpdata.(heights{h}).(bins{i})(~any(isnan(inpdata.(heights{h}).(bins{i})),2),:);
inpdata.(heights{h}).(bins{i}) = inpdata.(heights{h}).(bins{i})(~any(isinf(inpdata.(heights{h}).(bins{i})),2),:);
if isempty(inpdata.(heights{h}).(bins{i}))
inpdata.(heights{h}) = rmfield(inpdata.(heights{h}),(bins{i}));
else
params{i} = polyfit(inpdata.(heights{h}).(bins{i})(:,3), inpdata.(heights{h}).(bins{i})(:,4), 1);
inpdata.(heights{h}).(bins{i})(:,5) = real(params{i,1}(1,1)*inpdata.(heights{h}).(bins{i})(:,3) + params{i,1}(1,2));
end
[inpdata.(heights{h}).(names{1,i})]= params{i,1};
end
end
The code works in every regard except for ignoring empty fields. Should I add an if statement in my loop to account for empty cells? I tried as seen above with no luck.
Thanks for your time!
  1 commentaire
mashtine
mashtine le 26 Août 2014
I solved this by deleting the fields first and then applying all the other calculations that were not dependent on the field being empty of not but they cause my code to exceed the matrix dimensions.
Thank you nonetheless

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 26 Août 2014
Modifié(e) : Matt J le 26 Août 2014
If inpdata.(heights{h}).(bins{i}) is initially empty, it will still be empty after you assign [] to it, as you do in this part of the code:
if isempty(inpdata.(heights{h}).(bins{i}))
inpdata.(heights{h}).(bins{i}) = [];
else
You haven't changed anything...
  2 commentaires
mashtine
mashtine le 26 Août 2014
Modifié(e) : mashtine le 26 Août 2014
Yes, just noticed this sorry, trying to figure out how to incorporate rmfield into this. I tried the following with no luck either:
if isempty(inpdata.(heights{h}).(bins{i}))
inpdata.(heights{h}) = rmfield(inpdata.(heights{h}),(bins{i}));
else
params{i} = polyfit(inpdata.(heights{h}).(bins{i})(:,3), inpdata.(heights{h}).(bins{i})(:,4), 1);
inpdata.(heights{h}).(bins{i})(:,5) = real(params{i,1}(1,1)*inpdata.(heights{h}).(bins{i})(:,3) + params{i,1}(1,2));
end
This works on its own but doesn't seem to go through in the loop
mashtine
mashtine le 26 Août 2014
Solved, thank you Matt J

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by