Concatenating data from a variable within a MAT file in a loop

Within a loop, I want to 1) load a variable from a MAT file, 2) concatenate the newly loaded two-column data of varying lengths to a new variable array, and 3) save the variable to a new MAT file. I am fairly new to Matlab, but while I know how to add values to a given vector/array within a loop, this question has vexed me. Any suggestions?
EXAMPLE:
a=[load('fileA.mat','delta_length')];
b=[load('fileB.mat','delta_length')];
c=[load('fileB.mat','delta_length')];
new = [a.delta_length; b.delta_length; c.delta_length];
save('fileABC.mat','new');
In the above example, this is how I am doing it line by line, but the number of files I want to load could range from 1 to 10 or higher.
With my limited understanding I am trying something like this:
new=[];
for i=1:3
a_i=[load('fileA.mat','delta_length')];
new = [a_i.delta_length; ??how to keep appending a vector here??];
end
save('fileABC.mat','new');
Hopefully it's clear from this what I am trying to do. Any help much appreciated!
-Jason

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 3 de Ag. de 2013
Editada: Azzi Abdelmalek el 3 de Ag. de 2013
new = [new;a_i.delta_length]
% Use also sprintf to generate fileA,fileB,...
new=[];
s='A':'C'
for i=1:3
a_i=[load(sprintf('file%s.mat',s(i)),'delta_length')];
new = [new;a_i.delta_length];
end

3 comentarios

Peds IT
Peds IT el 3 de Ag. de 2013
Editada: Azzi Abdelmalek el 3 de Ag. de 2013
thanks for the answer - although i think my question wasn't quite clear, your answer still helped me figure out that at least the part i was struggling with was actually ok! plus, i learned something new.
for closure - here is my final code:
[files,pathname] = uigetfile('*.mat', 'Select MAT files (use CTRL/COMM or SHIFT)', ...
'MultiSelect', 'on');
source_file = char(files(1,1));
animal_ID = source_file(1:11); %just grabbing needed identifying text
delta_length=[];
for i=1:numel(files)
a=[load(files{1,i},'delta_length')];
delta_length = [delta_length; a.delta_length];
end
and 'delta_length' are just data from variables loaded from each file in the list that look something like this: e.g.: delta_length = [0 0 0;1 5 7]'; delta_length = [0 0 0 0 0; -2 3 0 10 100]'; etc....
thanks again...
Is it a question or just a comment?
just a comment with my solution. thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by