# Loop through tables in a cell array

13 views (last 30 days)
Fabian Niederreiter on 6 Apr 2021
Commented: Star Strider on 6 Apr 2021
I have two 41x1 cell arrays 'DS_10B' and 'DS_10T' containing 41 different tables each. The tables contain stock return data from column 4 onwards. (find the data attached)
First I want to loop through all tables of the array 'DS_10B' and calculate the mean stock return. I don't care about the mean return per table or column but only need the overall mean return of all the combined data in the array.
Then I want to repeat the exact same procedure for the other array 'DS_10T'.
I already managed to calculate the mean of one single table's stock return data with this:
% Extract test table
t = DS_10B{1,1};
% Extract relevant columns of test table
% Get all columns with stock return data of table as a column vectors of doubles
lastColumns = t{:,4:end};
% Reshape into one single column in order to calculate mean
lastCo = reshape(lastColumns,[],1);
% Mean the last column but use omitnan
mean_table = mean(lastCo,'omitnan');
Would be delighted if someone could help me with the for loop.

Fabian Niederreiter on 6 Apr 2021
Managed to solve it myself! :)
Here's my solution for everyone with a similar problem:
%% Mean Bottom Portfolio
meanDS_10B = NaN(1,numel(DS_10B));
for k =1:numel(DS_10B)
t = DS_10B{k,:};
% Extract stock data columns of every table
lastColumn = t{:, 4:end}; % Get all columns with stock return data of table as column vectors of doubles
%reshape into one single column in order to calculate mean
lastCo = reshape(lastColumn,[],1);
% mean the last column but use omitnan
mean_column = mean(lastCo,'omitnan');
meanDS_10B(k) = mean_column;
end
% Average all Means
avgRET_Bottom = mean(meanDS_10B,'omitnan');
Star Strider on 6 Apr 2021
No worries!
My problem is that although I was active in the U.S. stock market many years ago (out for more than a decade), I never did any technical analysis, and since economics and such is far from my areas of expertise, had no idea what you wanted. The MATLAB-related stuff was straightforward, everything else, not so much.