Problems running a function in a for loop and dynamically assigning to a struct

1 回表示 (過去 30 日間)
Hello Community,
Could anyone help with this please. I have a function (HF) that I want to run in a for loop across various data in a struct. The function is a logical mask and works as intended - but I want to automate a bit of processing hence the loop. As this is a processing exercise, I also need to record the output to a new struct, hence the dynamic assignment to the struct which I believe is the way forward. The code is:
for i = 1:numel(plno)
x = HF(S.Plt,(i),S.HN,10);
s.pl(i) = x
end
where plno are a series of plot numbers, S.Plt and S.HN are data from an existing struct 'S'. I want the loop to run across all instances of S.Plt with the relevant number from 'i' eg S.Plt(1), S.Plt(2) etc., the function HF does its masking, then the output to be written to a new struct 's' with s.pl(1), s.pl(2) etc. as the result.
I've not got the hang of this dynamic assigning despite many attempts. I do get an output for s.pl(1) which is as expected/correct, but my loop stops and doesn't run the rest, so can anyone help please?
Thanks,
10B.
  14 件のコメント
10B
10B 2016 年 11 月 17 日
Alexandra - you deserve a medal! Perhaps not beautiful - but it ultimately does what I couldn't acheive without your help. Perhaps one day Matlab and I will have a better relationship!
Many thanks for your help...
10B.
Alexandra Harkai
Alexandra Harkai 2016 年 11 月 17 日
Happy to help. Could have suspected that earlier... :)

サインインしてコメントする。

採用された回答

Alexandra Harkai
Alexandra Harkai 2016 年 11 月 17 日
Thought I'd add an 'Answer' to make this question 'answered' instead of the comments.
The loop fails eventually due to the HF function returning different sized arrays as output, which can't be assigned to the non-scalar struct, hence yielding an error, see details in comments.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by