Good night,
I have the following item in a estructure:
U.U_1np_2np_pp; U.U_1np_2np_pf; U.U_1np_2np_fp; U.U_1np_2np_ff;
Then I have another processes to do and I want to systematise it. If I just write their names in the output it will print their values. But if I want to obtain their values using a loop with varget
:
for hh_Ma (1,2,1); for hh_Fe (1,2,1); Utility =varget(U."U_1np_2np_"$+hh_prod[hh_Ma]$+hh_prod[hh_Fe]); endfor; endfor;
the following mistake arises: G0504 : Invalid structure member
If I try the following
for hh_Ma (1,2,1); for hh_Fe (1,2,1); Utility =varget("U.U_1np_2np_"$+hh_prod[hh_Ma]$+hh_prod[hh_Fe]); endfor; endfor;
This mistake shows up: G0025 : Undefined symbol 'U.U_1np_2np_pp'
How can I code this?
Thank you so much
1 Answer
0
It is usually best practice to avoid the use of varget
. varget
does not support the retrieval of structures or structure members. I think you would be much better off creating a vector or matrix of structure members and referencing with normal indexing. I do not know exactly what is in your structure, but here is the general concept
//Define structures struct Udata { matrix data; }; struct mystruct { struct Udata U_1np_2np; }; //Declare 'U' to be a 'mystruct' instance struct mystruct U; //Reshape the 'U_1np_2np' member into an array of structures U.U_1np_2np = reshape(U.U_1np_2np, 2, 2); //fill in data for i(1, rows(U.U_1np_2np), 1); for j(1, cols(U.U_1np_2np), 1); U.U_1np_2np[i,j].data = i + j / 10; endfor; endfor; //Extract data for i(1, rows(U.U_1np_2np), 1); for j(1, cols(U.U_1np_2np), 1); Utility = U.U_1np_2np[i,j].data ; endfor; endfor;
and if it is helpful to have the pf
, pp
, ff
, fp
information, you can add that to the substruct like this
//Make sure to clear workspace since we are changing //the definition of the 'Udata' struct new; //Define structures struct Udata { matrix data; string name; }; struct mystruct { struct Udata U_1np_2np; }; //Declare 'U' to be a 'mystruct' instance struct mystruct U; //Reshape the 'U_1np_2np' member into an array of structures U.U_1np_2np = reshape(U.U_1np_2np, 2, 2); names = ("pp" $~ "pf") $| ("fp" $~ "ff"); //fill in data for i(1, rows(U.U_1np_2np), 1); for j(1, cols(U.U_1np_2np), 1); U.U_1np_2np[i,j].data = i + j / 10; U.U_1np_2np[i,j].name = names[i,j]; endfor; endfor; //Extract data for i(1, rows(U.U_1np_2np), 1); for j(1, cols(U.U_1np_2np), 1); Utility = U.U_1np_2np[i,j].data ; endfor; endfor;
Your Answer
1 Answer
It is usually best practice to avoid the use of varget
. varget
does not support the retrieval of structures or structure members. I think you would be much better off creating a vector or matrix of structure members and referencing with normal indexing. I do not know exactly what is in your structure, but here is the general concept
//Define structures struct Udata { matrix data; }; struct mystruct { struct Udata U_1np_2np; }; //Declare 'U' to be a 'mystruct' instance struct mystruct U; //Reshape the 'U_1np_2np' member into an array of structures U.U_1np_2np = reshape(U.U_1np_2np, 2, 2); //fill in data for i(1, rows(U.U_1np_2np), 1); for j(1, cols(U.U_1np_2np), 1); U.U_1np_2np[i,j].data = i + j / 10; endfor; endfor; //Extract data for i(1, rows(U.U_1np_2np), 1); for j(1, cols(U.U_1np_2np), 1); Utility = U.U_1np_2np[i,j].data ; endfor; endfor;
and if it is helpful to have the pf
, pp
, ff
, fp
information, you can add that to the substruct like this
//Make sure to clear workspace since we are changing //the definition of the 'Udata' struct new; //Define structures struct Udata { matrix data; string name; }; struct mystruct { struct Udata U_1np_2np; }; //Declare 'U' to be a 'mystruct' instance struct mystruct U; //Reshape the 'U_1np_2np' member into an array of structures U.U_1np_2np = reshape(U.U_1np_2np, 2, 2); names = ("pp" $~ "pf") $| ("fp" $~ "ff"); //fill in data for i(1, rows(U.U_1np_2np), 1); for j(1, cols(U.U_1np_2np), 1); U.U_1np_2np[i,j].data = i + j / 10; U.U_1np_2np[i,j].name = names[i,j]; endfor; endfor; //Extract data for i(1, rows(U.U_1np_2np), 1); for j(1, cols(U.U_1np_2np), 1); Utility = U.U_1np_2np[i,j].data ; endfor; endfor;