MATLAB: Table calculations, annual return

annual returnerrorlooptabletable calculation

Im trying to calculate annual return of several stocks in a table by dividing the 12th observation on the first one, and get an error message on this code:
table = table();
table.AAPL = mean(AAPL,2);
table.AMZN = mean(AMZN,2);
table.F = mean(F,2);
table.INTC = mean(INTC,2);
table.MSFT = mean(MSFT,2);
table.TXN = mean(TXN,2);
table.UIS = mean(UIS,2);
table.XOM = mean(XOM,2);
ann_ret = table(12,:)./table(1,:);
When i write table(12,:) in the command window i get no error message, but when i try to run the code i get this error.
Error:
Subscripting into a table using one subscript (as in t(i)) or three or more subscripts (as in t(i,j,k)) is not
supported. Always specify a row subscript and a variable subscript, as in t(rows,vars).
If anyone could make a loop of some sort that would be nice as i need to calculate the annual returns for three years, meaning observation 24 and 36 from the table as well.

Best Answer

Assuming that you created the table called ‘T1’ correctly and that it contains variables (columns) named ‘AAPL’, ‘AMZN’ and the rest, do something like this to create a second table ‘T2’ with the results:
T2.AAPL = mean(T1.AAPL);
T2.AMZN = mean(T1.AMZN);
and so for the rest.
Also, naming it ‘table’ overshadows the actual table function, so it will not be possible to create more table arrays later. Name it something else, as I did in this example.