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

1 view (last 30 days)
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 Comments
10B
10B on 17 Nov 2016
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.

Sign in to comment.

Accepted Answer

Alexandra Harkai
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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!