Delete empty field from struct to allow polyfit to work

1 visualización (últimos 30 días)
mashtine
mashtine el 26 de Ag. de 2014
Comentada: mashtine el 26 de Ag. de 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 comentario
mashtine
mashtine el 26 de Ag. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 26 de Ag. de 2014
Editada: Matt J el 26 de Ag. de 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 comentarios
mashtine
mashtine el 26 de Ag. de 2014
Editada: mashtine el 26 de Ag. de 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 el 26 de Ag. de 2014
Solved, thank you Matt J

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by