Loop over table variables in MATLAB

5 vues (au cours des 30 derniers jours)
Jared
Jared le 13 Juil 2014
Modifié(e) : per isakson le 14 Juil 2014
Hi there,
I'm relatively new to MATLAB and I've been exploring the use of tables in MATLAB since I deal with quite a bit of Excel-based financial data that I'm importing into MATLAB. I'm trying to understand how to iterate over table variables in Excel. For example assumed I have table variables A=ExcelData.VFINX, B=ExcelData.VISVX and C=ExcelData.VIVAX and I want to do something like the following:
for A to B to C
regress(A,X)
end
Now, before you ask why I'd want to do this it's because I have a very large number of variables that I need to run the same regression on and I'd prefer to be able to set this up succinctly in a loop instead of writing out the same regression over and over and over except with a different LHS variable.
Thanks,
JK

Réponse acceptée

per isakson
per isakson le 13 Juil 2014
Modifié(e) : per isakson le 14 Juil 2014
A = ExcelData.VFINX;
B = ExcelData.VISVX;
C = ExcelData.VIVAX;
with a cell array
for cac = {A,B,C}
regress( cac{:}, X )
end
or without A, B and C
data = { ExcelData.VFINX, ExcelData.VISVX, ExcelData.VIVAX };
for cac = data
regress( cac{:}, X )
end
or with a struct
SA=struct('A',ExcelData.VFINX,'B',ExcelData.VISVX,'C',ExcelData.VIVAX);
for cac = reshape( fieldnames( SA ), 1, [] )
regress( SA.(cac{:}), X )
end
&nbsp
EDIT
I added
  • reshape to the struct case to convert the column, which is returned by fieldnames, to a row, which is required by the for-loop.
  • {:} to the struct case
to fix a couple of mistakes

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by