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 CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by