Loop over table variables in MATLAB

6 visualizaciones (últimos 30 días)
Jared
Jared el 13 de Jul. de 2014
Editada: per isakson el 14 de Jul. de 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

Respuesta aceptada

per isakson
per isakson el 13 de Jul. de 2014
Editada: per isakson el 14 de Jul. de 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

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by