Looping command for vectors with common string prefix
1 view (last 30 days)
Show older comments
Dear all,
I need to iterate the same command for a list of vectors with common string prefix and a different number that characterise each. For instance, I have a market average "avilliq" and a list of vectors named milliq1, milliq2, ... , milliq91.
I need to run a MS_Regress_Fit and extract the parameters for each of these vectors. The code for one of them would be something like:
lnavilliq(isnan(lnavilliq))=[];
milliq1(isnan(milliq1))=[];
% Matrix of dependent vars
c = [lnavilliq milliq1];
dep= c; % Defines the dependent variables in system
constVec=ones(length(dep),1);
indep{1}=constVec;
indep{2}=[constVec lnavilliq];
S{1}=[1 1];
S{2}=[1 1 1];
k=2; % Number of states/regimes
[Spec_Out_1]=MS_Regress_Fit(dep,indep,k,S,advOpt);
% Extract vector of parameters (betas)
c_high=Spec_Out_1.param(9);
c_low=Spec_Out_1.param(10);
beta2_high = Spec_Out_1.param(11);
beta2_low = Spec_Out_1.param(12);
%Extracts transition matrix from parameter vector
beta2_p11=Spec_Out_1.Coeff.p(1);
beta2_p22=Spec_Out_1.Coeff.p(4);
I need to iterate Spec_out_# for each milliq# and then extract the parameters c_high_#, c_low_# and so on.
Any suggestion?
Thanks
SG
3 Comments
Stephen23
on 30 Aug 2018
Edited: Stephen23
on 30 Aug 2018
"Any suggestion?"
Do NOT do that. Magically accessing/defining variabel names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know more:
"Where is the problem?"
If the fieldname is PortAvilliq1, then your code will not magically add some number onto the end of the fieldname:
data.PortAvilliq(n)
What your code actually does is accesses n-th element of the field named PortAvilliq. Because this field (probably) does not exist you get an error. See my answer to know how to define the fieldname dynamically.
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!