Problems running a function in a for loop and dynamically assigning to a struct
Ältere Kommentare anzeigen
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 Kommentare
Alexandra Harkai
am 17 Nov. 2016
Does it give an error anywhere?
Alexandra Harkai
am 17 Nov. 2016
Bearbeitet: Alexandra Harkai
am 17 Nov. 2016
Where exactly does the error occur (which function, which line)?
Before you run the code again, you can
dbstop if error
which will stop the code at the problem line where you can check which assignment is erroneous.
10B
am 17 Nov. 2016
Alexandra Harkai
am 17 Nov. 2016
You could skip x entirely and just write:
s.pl(i) = HF(S.Plt,(i),S.HN,10);
10B
am 17 Nov. 2016
Alexandra Harkai
am 17 Nov. 2016
Does
dbstop if error
not point to you a particular line either?
Have you tried to put a breakpoint inside the loop and see what values does HF() return?
10B
am 17 Nov. 2016
Alexandra Harkai
am 17 Nov. 2016
This sounds like the usual behaviour of the for loop (in the sense that it loops through the array 1:26).
However: did you check if HgtFilter returns the same size result for all of the i values?
10B
am 17 Nov. 2016
Alexandra Harkai
am 17 Nov. 2016
Bearbeitet: Alexandra Harkai
am 17 Nov. 2016
Depends what you aim to do with the results. A very simple (but not necessarily beautiful) workaround is to whack them in a cell, like so:
N = numel(plno);
s.pl = cell(N, 1);
for i = 1:N
s.pl(i) = {HgtFilter(S.Plot,(i),S.Height_Norm,10)};
end
Or: you can pad them out with NaN or 0s to make them all the same size. (Note that you may not even need a struct to collect the results this way after all: if you pad them out, you can fill in the rows/columns of an array; if you put them in a cell, the result can be a cell array.)
Or: Use a non-scalar struct but let them all have a field for these results:
for i = 1:numel(plno)
s.pl(i).x = HgtFilter(S.Plot,(i),S.Height_Norm,10);
end
10B
am 17 Nov. 2016
Alexandra Harkai
am 17 Nov. 2016
Happy to help. Could have suspected that earlier... :)
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!