Summary: I want to call a function that returns multiple structs n
times. How can I append the results to existing fields in my output structs (i.e. create vectors) instead of creating new fields that contain a scalar each iteration?
Example: Consider a function sample_fct( x )
that 1) perfroms some operations on x
and saves the result in a couple of new variables (a
and b
in the sample code) and then 2) calls some sub-functions calculate_one( x )
and calculate_two( x )
with a
and b
as input. It doesn't matter what exactly these functions do. Output of these functions is then collected in struct A
and B
.
function [A, B] = sample_fct( x )
a = 1 * x;
b = 2 * x;
[A.one, A.two] = call_functions( a );
[B.one, B.two] = call_functions( b );
function [one, two] = call_functions( input )
one = calculate_one( input );
two = calculate_two( input );
function one = calculate_one( input )
one = input.^2;
end
function two = calculate_two( input )
two = input.^3;
end
end
end
I then want to call this function n
times with different input parameters in my script
n = 3;
for i = 1:n
[A(i), B(i)] = sample_fct( i );
end
When I do this, A
and B
become 1*n structs, each field again containing the fields one
and two
. So in my example with n = 3
I have 3 instances of scalars one
and two
. The output of my sample code looks like this:
>> A
A =
1x3 struct array with fields:
one
two
>> A.one
ans =
1
ans =
4
ans =
9
What I actually want is A
and B
to be 1*2 structs with 1*n vectors one
and two
, so the desired output should look something like this:
>> A
A =
two: [1 8 27]
one: [1 4 9]
How exactly can I do this without [one, two] being my function's output variable and without calling my function for A and B seperately?
Why I want to do this: I want to run a predictive model with different parameter combinations on a time series and calculate some goodness-of-fit measures and other statistics for 1 minute, 1 hour, 1 day, etc. representations. In my example, x
would be the time series, the loop over n
a loop over different parameter vectors, a
and b
representations with different sampling time and one and two
some statistics that I want to gather in structs A
and B
. I'm pretty sure theres a much more sophisticated way to do this but I just can't wrap my head around it.
I know this is easy to do with vectors/matrices instead of structs, but I would like to be able to call my output with a variable name instead of A.hourly(:,19) or something like that, since I calculate many many statistics and not just two in my actual code.