Problems running a function in a for loop and dynamically assigning to a struct
1 view (last 30 days)
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
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?
Alexandra Harkai on 17 Nov 2016
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.