Delete empty field from struct to allow polyfit to work

조회 수: 1 (최근 30일)
mashtine
mashtine 2014년 8월 26일
댓글: mashtine 2014년 8월 26일
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
mashtine
mashtine 2014년 8월 26일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2014년 8월 26일
편집: Matt J 2014년 8월 26일
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
mashtine
mashtine 2014년 8월 26일
편집: mashtine 2014년 8월 26일
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 2014년 8월 26일
Solved, thank you Matt J

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by