Loop over table variables in MATLAB

7 ビュー (過去 30 日間)
Jared
Jared 2014 年 7 月 13 日
編集済み: per isakson 2014 年 7 月 14 日
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 Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by