MATLAB: Inserting blank columns at specific points

columnempty-columninsert

So I have a set of data that I wish to take the means of. The first set worked fine for me as there were 22 values, where there were 11 items with 2 repeats. The issue I am now having is that my second set has 29 values, where there are 10 items, where 9 items have 3 repeats and a annoying 1 item has 2 repeats.
for j = 3:16
for i=1:21
MeanR{j,i} = mean(Numbers(j,i:(i+1)));
end
end
MeanR(:,2:2:end)=[];
I am not sure if there was an easier way, but I wanted mean values of 1&2, 3&4 etc, but this takes means values of 1&2, 2&3 etc so I just deleted every second item. But this way will not work for my second set. I tried to insert an empty column so I can use the same method, but I cannot seem to do so. Any ideas?
I have attached the file of data that I am using. I use xls read to extract the data into numbers and text, then from that work out means

Best Answer

% import data to Matlab as a Table
T_data = readtable('Data_of_interest.csv');
% get rid of meaningless columns/rows
T_data(1,:) = [];
T_data(:,53:end) = [];
% 1st column seems to be a description so use it as such
T_data.Properties.RowNames = T_data.(1);
T_data.(1) = [];
% Split the data into
% - SET1: first 22 columns
% - SET2: the rest
T_set1 = T_data(:,1:22);
T_set2 = T_data(:,23:end);
% Let's focus now on the 'T_set2' as it has irregularl
% number of the repeats
% One way to deal with the problem is to count
% the uniqe tests and their repeats.
% Your column names have a fixed format'.
% Make sure you only keep the relevant bit of the name, that is:
% 'x201X_XX_XX' = 11 CHARS
% If you loose the suffix, you'll be able to determine:
% 1) how many unique names you have
% 2) what is the number of their repeats
% 3) their location within table
% get the colum names
columNames = T_set2.Properties.VariableNames;
% transpose them for easier human readibility
columNames = columNames'
% remove the suffix
for i=1:length(columNames)
% before truncation
columNames(i)
columNames{i}(11+1:end) = [];
%after truncation
columNames(i)
end
% find unique names (logical vector)
uniqueNameCounter = false(length(columNames),1);
uniqueNameCounter(1) = true;
for i=2:length(columNames)
if columNames{i}==columNames{i-1}
uniqueNameCounter(i) = false;
else
uniqueNameCounter(i) = true;
end
end
% now use this logical vector to find the indexes
% of your uniqe tests
allindexes = 1:size(T_set2,2);
uniqueIndexes = allindexes(uniqueNameCounter);
% now use 'uniqueIndexes' to get the number
% of the repeats per test
noOfrepeats = uniqueIndexes(2:end)-uniqueIndexes(1:end-1)
% notice that you have 10 unique names but 9 'noOfRepeats'
% 10th number of the repeats is produced in a following way:
noOfrepeats(end+1) = size(T_set2,2) - uniqueIndexes(end)+1
% Now that you know where your uniqe names are
% And the number of the repeats per unique name
% You can create a code that accesses these data
% And applies whatever calculation you need
% i.e. Add MEAN
% preallocate memory for your calcualtion
tempMat = zeros(size(T_data,1),length(uniqueIndexes));
% calculate MEAN
for i = 1:length(uniqueIndexes)
% show selected fragment of the table
T_set2(:,uniqueIndexes(i):uniqueIndexes(i)+noOfrepeats(i)-1)
% make room for new data
aveVal = mean(T_set2{:,uniqueIndexes(i):uniqueIndexes(i)+noOfrepeats(i)-1},2);
tempMat(:,i) = aveVal
end
% Store MEAN results to a table
T_AveVal = array2table(tempMat);
% new column names
newNames = cell(1, length(uniqueIndexes));
for i=1:length(uniqueIndexes)
newNames{i}= [columNames{uniqueIndexes(i)} '_Mean']
end
T_AveVal.Properties.VariableNames = newNames;
% Display MEAN
T_AveVal