MATLAB: Separating Data into columns based on certain text variable in that data

parsingseparation

I have a list of daily stock returns for each stock on the S&P 500. The returns are formatted so that each stock is grouped together like this:
1/1/2013 AAPL .005%
1/2/2013 AAPL -.1%
….
1/1/2013 GOOG .5%
1/2/2013 GOOG .25%
How would I sort this so that the each stock can be a column name and have its returns below it?
Thanks!

Best Answer

>> data=readtable('jack.dat','format','%{MM/dd/yyyy}D %s %f%%')
data =
Var1 Var2 Var3
__________ ______ _____
01/01/2013 'AAPL' 0.005
01/02/2013 'AAPL' -0.1
01/01/2013 'GOOG' 0.5
01/02/2013 'GOOG' 0.25
>> [u,~,ic]=unique(data.Var2);
>> stks=table(data.Var1(ic==1)); % get the dates first; must all be same
>> for i=1:length(u) % get each individual stock
stks(:,i+1)=table(data.Var3(ic==i));end
>> stks.Properties.VariableNames=[{'Date'}, u.'] % add useful names
stks =
Date AAPL GOOG
__________ _____ ____
01/01/2013 0.005 0.5
01/02/2013 -0.1 0.25
>> clear data % done with it...
Above does assume that every stock has the identical timestamp; any missing will cause failure.