Iterating in a dataset encompassing a structure

조회 수: 1 (최근 30일)
Davin
Davin 2014년 10월 21일
댓글: Davin 2014년 10월 21일
Hello all,
I have an issue accessing an array in a dataset table which emcompasses a struct in it.
I want to build an array which contains eigenvectors from different rank during the Johansen test.
I have the following :
basevar = evalin('base', x)
[c,n] = size(basevar)
arrcoef = zeros(n)
[h,pvalue,stat,cValue,mles] = jcitest(basevar,'model','H1', 'lags', 1)
Here i get, a dataset called mles.
Inside this dataset I have :
mles =
r0 r1
t1 [1x1 struct] [1x1 struct]
and when i do mles.r0 I have
paramNames: {5x1 cell}
paramVals: [1x1 struct]
res: [1498x2 double]
EstCov: [2x2 double]
eigVal: 0.0106
eigVec: [2x1 double]
rLL: -1.2002e+03
uLL: -1.1902e+03
Here I want to get the eigVec array.
So i open a loop :
for i = 1:n
coeff = double(pvalue(:,i))
if 0.05 - coeff && 0.05 - coeff > 0
arcoeff(i) = mles.r(i).eigVec
end
end
I can see the issue it comes from this part : mles.r(i), it takes r as a variable which is normal, I want to know how to make this .r0 reactive to an iteration. I tried with {} and putting the name in something like this
a = mles.Properties.VarNames{i}
mles.a.eigVec
But this does not work.
Do you know how can make this reactive to my iterative value i?
thanks
D

채택된 답변

Matt Tearle
Matt Tearle 2014년 10월 21일
편집: Matt Tearle 2014년 10월 21일
If I understand the problem correctly, you're getting a dataset with variables r0, r1, foo, potato, etc, each one of which contains a struct, and you want to extract the eigVal field from each variable. But the problem is that you can't find a way to loop over the variable names programmatically.
Assuming that's the issue...
Firstly, if you can use 13b or later, you can use tables rather than datasets, which are a bit nicer and allow either named or numeric indexing with {}:
for i = 1:n
arcoeff(i) = mles{:,i}.eigVec;
end
But, either way, you can use dynamic variable referencing in the same way you can with structs:
x = dataset(rand(5,1),rand(5,1))
y = 'Var2'
z = x.(y) % Equivalent to z = x.Var2
So now
vnames = mles.Properties.VarNames;
for i = 1:n
arcoeff(i) = mles.(vnames{i}).eigVec;
end
[Side note: given that the eigenvectors are, well, vectors, I think your indexing will have to be something like arcoeff(:,i) = ...]
[Second side note: the condition if 0.05 - coeff && 0.05 - coeff > 0 isn't well formed: the first part 0.05 - coeff is numeric, so will be converted to logical, which will make it equivalent to 0.05 - coeff > 0 (so you're testing the same thing twice -- "if X && X").]
  댓글 수: 1
Davin
Davin 2014년 10월 21일
Thanks Matt for yr answer. You are also right on the last comment.
cheers

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2014년 10월 21일
편집: Guillaume 2014년 10월 21일
Use dynamic field names for accessing your rank variables:
arcoeff{i} = mles.(sprintf('r%d', i)).eigVec;

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by