Store several output matrices in a single matrix
1 view (last 30 days)
Show older comments
Hi everybody!
I have 205 .mat files of size 31590x6 and I have a script that takes the averages every 15 minutes so that every file is reduced to a size 34x6. So my main goal after calculating the 15 min intervals is to put all these reduced files into a single matrix that I can use later for other calculations.
The following script does the interval calculations without problems, but when I triy to put all the Oupt matrices of each file in a single matrix, it doesnt work :( because matlab says : ??? Index exceeds matrix dimensions.
All_Data=[];
dataFiles=dir('*.mat');
for d=1:length(dataFiles)
file=dataFiles(d).name; %gets names of all mat files
fileData=load(file);
Data=fileData(d).finalData;
%NAME DATE, STOCK AND TIMESTAMP
date=Data(2:end,1);
timestamp=Data(2:end,2);
stock=Data(2:end,3);
%NAME BID VARIABLES
bid1=cell2mat(Data(2:end,4));
%NAME ASK VARIABLES
ask1=cell2mat(Data(2:end,14));
%NAME BID VOLUME
bvol1=cell2mat(Data(2:end,24));
%NAME ASK VOLUME
avol1=cell2mat(Data(2:end,34));
%CONVERT TIME STAMP AND DATE INTO NUMERIC VALUE
dateNum=datenum([date{:}],'dd.mm.yy');
timeNum=datenum([timestamp{:}],'HH:MM:SS');
%CALCULATE TIME INTERVALS
t = dateNum+timeNum;
t15 = datenum('00:15:00','HH:MM:SS')-datenum('00:00:00','HH:MM:SS');
ngroups = ceil((max(t)-min(t))/t15); %number of bins
findfirsttime =find(timeNum,1,'first');
firsttimestamp=timeNum(findfirsttime);
differences=zeros(size(t)); %assign memory
differences = timeNum(:)-firsttimestamp(:);
binNumbers=differences*96;
FifteenMinBins=floor(binNumbers);
%CALCULATE BID MEANS
b1=accumarray(1+FifteenMinBins(:),bid1(:),[],@mean);
%CALCULATE ASK MEANS
a1=accumarray(1+FifteenMinBins(:),ask1(:),[],@mean);
%CALCULATE BID VOL MEANS
bv1=accumarray(1+FifteenMinBins(:),bvol1(:),[],@mean);
%CALCULATE ASK VOL MEANS
av1=accumarray(1+FifteenMinBins(:),avol1(:),[],@mean);
%CALCULATE TIME INTERVAL FOR TIME
timeinterval=accumarray(1+FifteenMinBins(:),timeNum(:),[],@mean);
%GET FIRST 34 NUMERIC VALUES OF DATE
getdatenum=dateNum(1:34);
%COLLECT OUTPUT IN MATRIX
%Outtput=[getdatenum timeinterval b1 a1 bv1 av1];
%save('Output(d).mat',Output)
%STORE OUTPUT IN A MATRIX
Output=[getdatenum timeinterval b1 a1 bv1 av1];
%for i=1:length(dataFiles)
Output=[getdatenum timeinterval b1 a1 bv1 av1];%i try w/4 var
All_Data=[All_Data;Output];
end
The error I get is the following :??? Index exceeds matrix dimensions. And when i check the All_Data matrix it just contains one matrix of size 34x6, but i cant tell if it is the first matrix or the last one when the scripts runs the part of calculating the time intervals for all those 205 files.
So in the end, the last part of the code is trying to get a matrix of size 6970x6; composed of the output matrices of all the reduced files.
Do you see something wrong in the code? I don´t understand what the problem is and I have been trying it so many times that i don´t k now what else to do.
I would apprecciate any help.
Thank you and have a nice day!
Lourdes
5 Comments
Oleg Komarov
on 17 May 2011
This is a duplicate to: http://www.mathworks.com/matlabcentral/answers/7548-problem-in-appending-matrices-in-vertical-form
Please don't post duplicates. Also, you didn't correct for my observations in the previous posts and and "end" is missing here and redundant "Output" line is added here.
Note that my comments on the other post is the same as Fangjun's.
Answers (1)
Fangjun Jiang
on 17 May 2011
You have a nested for-loop. You don't need the inner for-loop for this purpose.
You have the variable "All_Data" set to be empty at the beginning. You go through a loop for 205 files. Every loop you calculate the variable "Output" and then you append it to "All_Data". Your coding flow is sound.
Your code should work if you remove your inner for-loop. Just leave the line "All_Data=[All_Data;Output];" there.
A couple of advice: Don't use variable "date". It is a built-in function. Don't use confusing variable names like "All_Data" and then "All_data". In this case, "All_data" is never used. I believe it is a mistake. Remove it.
13 Comments
See Also
Categories
Find more on Whos 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!