Loop over table variables in MATLAB

10 次查看(过去 30 天)
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

采纳的回答

per isakson
per isakson 2014-7-13
编辑:per isakson 2014-7-14
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by