joining two arrays to make a longer one

If I have two arrays, lets say the first is called a and the second is called b, I am aware of the fact that I can combine them with the command A=[a;b] to get a longer array
the problem I have got now is the following: I have two matlab files (let's say the first one is year 2002 and the second one from year 2003) which are saved in my computer and wich I would like to load, what is no big deal. Ich matlab file consist of 8 vectors which are called F,K,PC,Price,T,VIX,date,r
loading this is no problem, but as soon as I an loading both files, because the arrays in the files have the same name, the second file is overwriting the first one is there a way to write a code in a script which loads both files and combines them the way I mentionned above without overwriting the first file?

 Accepted Answer

You aren't using load correctly. You need to load each into its own structure
storedStructure1 = load(filename1);
storedStructure2 = load(filename2);
% Get theResults (or whatever) from each
theResults1 = storedStructure1.theResults;
theResults2 = storedStructure2.theResults;
% Concatenate
theResults = [theResults1; theResults2];

More Answers (1)

Cedric
Cedric on 7 Apr 2013
Edited: Cedric on 7 Apr 2013
If all vectors are column vectors, do the following:
data = [] ;
for f = 1 : nFiles
% Load file f:
% ... whatever you are doing to load the file.
% CAT to data array:
data = vertcat(data, [F,K,PC,Price,T,VIX,date,r]) ;
end
if these are row vectors, just transpose them. If it is a mix and you don't want to investigate, just read them linearly ( => column ):
data = vertcat(data, [F(:),K(:),PC(:),Price(:),T(:),VIX(:),date(:),r(:)]) ;

10 Comments

data = [] ;
for f = 1 : nFiles
exampleFile1='SPX_2002.mat';
load(exampleFile1);
exampleFile2='SPX_2003.mat';
load(exampleFile2);
data = vertcat(data, [F(:),K(:),PC(:),Price(:),T(:),VIX(:),date(:),r(:)]) ;
end
I tired this, but it did not work, what am I doing wrong? F,K,PC, etc. are column-vectors
You should go for a solution around the following example:
years = 2002:2003 ;
data = [] ;
for yId = 1 : numel(years)
fname = sprintf('SPX_%d.mat', years(yId)) ;
load(fname) ;
data = vertcat(data, [F,K,PC,Price,T,VIX,date,r]) ;
end
I modified your code a little for what I am lookign for:
years = 2002:2003 ;
data = [] ;
for yId = 1 : numel(years)
fname = sprintf('SPX_%d.mat', years(yId));
load(fname) ;
data = vertcat(data, [F,K,PC,Price,T,date,r]) ;
end
but if I run the code, 2003 overwrites the 2002 data, it's not added
It is added in the array data, the other variables K, etc being just temporary variables. This code provides you directly with the array data that you have been using in your other questions.
if I run just the 2003 data, I get 46200 elemets for each vector and if I run the code you provided me, I get 46200 elemets as well, but I should get around twice as much, because I am looking for a matrix (data) with all the data from 2002 and 2003
If you run
clear all
exampleFile1 = 'SPX_2002.mat' ;
load(exampleFile1) ;
whos
is there a variable named data? If so, rename the variable data from my example into e.g. allData.
If you want to understand the principle, try/understand the following:
data = [] ;
for k = 1 : 5
data = vertcat(data, rand(2,3)) ;
end
size(data)
and you will see that it works.
if I run that:
clear all
exampleFile1 = 'SPX_2002.mat' ;
load(exampleFile1) ;
whos
There is no variable called data, the matrix data is created using this comand:
data=[F,K,PC,Price,T,date,r];
However, both series (2002 and 2003) consinst of 8 elements with the exact same names, for both years the 8 elements are called K, F, PC, Price, T, VIX, date, r
The problem is that if I load the series 2002 first and 2003 afterwards, due to the fact that in both dataset the same 8 elemets (K, F, PC, Price, T, VIX, date, r) are used, the second dataset overwrites the first one. the problem is not to construct that data, this I can do afterward if it's necessary, but I need to be able to load both datasets and to get a longer vector consisting firstly of all elements of 2002 (starting at 01.01 and ending on 31.12) and then of 2003 (starting 01.01 and ending 31.12)
I know that these are the same names, this is why I proposed to use them as temporary/intermediary variables and to build data directly in the loop. This way you have data defined as a concatenation of all relevant variables after the loop, and you can discard K, F, etc. If you kept the line
data = [F,K,PC,Price,T,date,r];
after the loop, this is the reason why you had only the last block of data previously (because this line overwrites the array data that is built in the loop).
However, even in this situation, Image Analyst's remark is right and I should have proposed the following that is cleaner:
years = 2002:2003 ;
data = [] ;
for yId = 1 : numel(years)
fname = sprintf('SPX_%d.mat', years(yId)) ;
ld = load(fname) ;
data = vertcat(data, [ld.K,ld.F,ld.PC,ld.Price,ld.T,ld.date,ld.r]) ;
end
great, that seems to work, thanks!
Locks
Locks on 8 Apr 2013
Edited: Locks on 8 Apr 2013
What I still do not reallly understand is how that is working, or in other words which element is allowing me to load the data without overwriting them, could you explain that to me? Is seems as 2002 is stored in ld, but I do not get what ld is and how it's done then

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!