- 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
Loop over table variables in MATLAB
10 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
per isakson
on 13 Jul 2014
Edited: per isakson
on 14 Jul 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
 
EDIT
I added
to fix a couple of mistakes
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!