フィルターのクリア

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 Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by