Building a Structure from a function and data from another strucutre
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I am having trouble building a structure out of a nested function, using data from another structure, and
then accessing the values for later calculations. I will write down my process as best I can, although
simplified.
The file containing parameters and variables for each member of the test data set:
Parameterfile.m
>>N=3; % number of members in the dataset. These codes will be used to run large dataset; 3 is just for
example.
>>i=1
>>Mid(i).var1=var1value1;
>>Mid(i).var2=var2value1;
>>Mid(i).var3=var3value1;
>>
>>i=2
>>Mid(i).var1=var1value2;
>>Mid(i).var2=var2value2;
>>Mid(i).var3=var3value2;
>>
>>i=3
>>Mid(i).var1=var1value3;
>>Mid(i).var2=var2value3;
>>Mid(i).var3=var3value3;
% etc.
>>
% This builds a 1x3 structure (Mid) for all of the variables for each of the members of the set (i=1, 2
and 3).
% Then, to use these fields in the build function, I define these fields as variables
>>
>>in1= Mid(i).var1;
>>in2= Mid(i).var2;
>>in3= Mid(i).var3;
% etc.
Masterscript.m
% First I call the file containing the parameters.
>>Parameterfile;
% then set up a loop through the members of the data set to build the structure with data from each
member
>>for i=1:N
>>Parameterfile;
>>[Out(i)]=build(in1,in2,in3);
>>end
% where Out is the structure I want to build to be used in later processing.
% I need to call the parameterfile again within the loop, otherwise the rest of the process only uses
i=N, and repeats all of the calculations for N times with the same data (because the structure Mid only
contains data from i=N)..
The build function (which is called in the Mastercript)
build.m
>>function [Out]=build(in1,in2,in3)
>>
>>parameterfile;
>>Out.in1=in1;
>>Out.in2=in2;
>>Out.in3=in3;
% calculations
>>Out.res1=result1;
>>Out.res2=result2;
% result1, result2, etc are saved in the structure to be used in further processing steps
>>end
The first structure, Mid, is created successfully, with all of the variables from each member, i=1:N
The second structure (Out) on the other hand is a 1x3 struct, where the first two fields (corresponding
to i=1 and i=2) are empty, and the last field is complete with the data from the i=3 member of the data
set.
From this point, a suite of calculations are performed using the data (result1, result2, etc) from the
structure Out, each set of calculations run on a loop from i=1:N.
I can actually go through with the entire process of calculations that follow utilizing the information
from the Out structure, but only calculations using the last field (i=N) are processed. Plots that
correspond to the empty fields just come out blank (as could be expected since the fields in the
structure are blank).
So, what I need is to have the structure Out save all of the fields for each member of the test (i=1:N-1),
not just for the last member (i=N).
Any input on what I am missing would be greatly appreciated.
0 Commenti
Risposte (1)
Aarti Ghatkesar
il 1 Ott 2014
This happens since when you create an out variable during a call to the build function, this out variable is created in the function workspace and will not be available in the next function call. Hence the behavior that you observe.
However if you pass the out variable created during the previous call to the build function in the next function call to the build variable then the issue is solved.
Please refer to the attachments for a sample code.
0 Commenti
Vedere anche
Categorie
Scopri di più su Get Started with MATLAB in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!