Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Concatenate in a loop

Subject: Concatenate in a loop

From: Sumeet T

Date: 12 Aug, 2010 16:40:27

Message: 1 of 5


Hi,

I am facing a trouble while concatenating in a loop. Here is a problem:

I have matrix B1, B2, B3, B4.....,B20 (each matrix has different number of rows but have same number of columns i.e. 3) and on each matrix I perform a certain operation. At the end of the operation I write the data to a txt file and clear everything else.

To process everything at one go, I wish to have a loop so that the code reads each matrix performs the operation, and moves to the next matrix. But I cant figure out the way to code. I have the following in my mind but do not know how to move forward:

for j=1:1:20
m= strcat('B', j);


.... Perform mathematical operation on matrix m
....

end

After concatenation of the B with j, m=Bj (j has a value like 1 or 2..or...), but m is not the same as Bj matrix. Can someone please help me figure out the correct solution to this problem. I very much appreciate your assistance.

Thanks so much.

Subject: Concatenate in a loop

From: Steven_Lord

Date: 12 Aug, 2010 19:07:02

Message: 2 of 5



"Sumeet T" <sumeettrehan@gmail.com> wrote in message
news:i4185r$nat$1@fred.mathworks.com...
>
> Hi,
>
> I am facing a trouble while concatenating in a loop. Here is a problem:
>
> I have matrix B1, B2, B3, B4.....,B20 (each matrix has different number of
> rows but have same number of columns i.e. 3)

Don't do this! See Q4.6 in the newsgroup FAQ for alternatives.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Concatenate in a loop

From: Jan Simon

Date: 12 Aug, 2010 22:46:04

Message: 3 of 5

Dear Sumeet,

> I have matrix B1, B2, B3, B4.....,B20

This structure yields to the difficulties you have. It is recommended not to mix the data structure and the data itself. You would not append the time-table to the name of a pupil, also there could be some advantages in doing so.

Better: Collect the arrays in a cell array: B{1}, B{2}, ...
Then the final concatenation is easy:
  CatB = cat(1, B{:});

Kind regards, Jan

Subject: Concatenate in a loop

From: Sumeet T

Date: 13 Aug, 2010 14:50:23

Message: 4 of 5


Thanks Steve and Jan! That was very helpful.


"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i41tjc$bav$1@fred.mathworks.com>...
> Dear Sumeet,
>
> > I have matrix B1, B2, B3, B4.....,B20
>
> This structure yields to the difficulties you have. It is recommended not to mix the data structure and the data itself. You would not append the time-table to the name of a pupil, also there could be some advantages in doing so.
>
> Better: Collect the arrays in a cell array: B{1}, B{2}, ...
> Then the final concatenation is easy:
> CatB = cat(1, B{:});
>
> Kind regards, Jan

Subject: Concatenate in a loop

From: Olivier

Date: 11 Oct, 2012 22:51:08

Message: 5 of 5

If I have a sequence of yearly time series data sets (data1998.mat data1999.mat ... data2009.mat), which contain the same variable names, and load them sequentially, like this:

files = dir('C:\Timeseries\data*.mat);
for i = 1:length(files)
    eval(['load ' files(i).name, ' time counts']);
end

... how can I concatenate the variables 'counts' from each file sequentially (in the for loop above, for instance) in the 'time' dimension, given that certain yearly data sets do not necessarily begin on Jan-1-YYYY, end on Dec-31-YYYY, or are missing altogether?

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us