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:
How to append a new row of data to the current matrix?

Subject: How to append a new row of data to the current matrix?

From: Kuo-Hsien

Date: 5 Sep, 2011 14:33:28

Message: 1 of 7

Dear my friends,

I want to keep appending a new row of data to the current matrix. How to do that?

Here is my code what I want to do, but it doesn't work. Please advice me. Thanks.

a = (1:5)'
b = (6:10)'

for n = 1:5
    c(n) = [a(n,:);b(n,:)]
end
c

Subject: How to append a new row of data to the current matrix?

From: ImageAnalyst

Date: 5 Sep, 2011 15:16:42

Message: 2 of 7

Are you thinking of this:
c=[a b]
??? or something else?

Subject: How to append a new row of data to the current matrix?

From: Luka Djigas

Date: 5 Sep, 2011 15:18:57

Message: 3 of 7

On Mon, 5 Sep 2011 14:33:28 +0000 (UTC), "Kuo-Hsien"
<mchangks@hotmail.com> wrote:

>Dear my friends,
>
>I want to keep appending a new row of data to the current matrix. How to do that?
>
>Here is my code what I want to do, but it doesn't work. Please advice me. Thanks.
>
>a = (1:5)'
>b = (6:10)'
>
>for n = 1:5
> c(n) = [a(n,:);b(n,:)]
>end
>c

Are you trying to accomplish something similar to this maybe?

a=(1:5)'
b=(6:10)'
c=[a,b]
c(end+1,:)=22


Luka

Subject: How to append a new row of data to the current matrix?

From: Anh Nguyen

Date: 5 Sep, 2011 19:56:46

Message: 4 of 7

On Sep 6, 12:33 am, "Kuo-Hsien" <mchan...@hotmail.com> wrote:
> Dear my friends,
>
> I want to keep appending a new row of data to the current matrix. How to do that?
>
> Here is my code what I want to do, but it doesn't work. Please advice me. Thanks.
>
> a = (1:5)'
> b = (6:10)'
>
> for n = 1:5
>     c(n) = [a(n,:);b(n,:)]
> end
> c

If for row you can do: c=cat(1,a,b)
For column you can do as previously advised or c=cat(2,a,b)

Subject: How to append a new row of data to the current matrix?

From: Kuo-Hsien

Date: 6 Sep, 2011 03:54:10

Message: 5 of 7

Anh Nguyen <theanh.nguyen@qut.edu.au> wrote in message <812df0b9-f72a-4b0a-86b5-4ef23adfeb4f@n19g2000prh.googlegroups.com>...
> On Sep 6, 12:33 am, "Kuo-Hsien" <mchan...@hotmail.com> wrote:
> > Dear my friends,
> >
> > I want to keep appending a new row of data to the current matrix. How to do that?
> >
> > Here is my code what I want to do, but it doesn't work. Please advice me. Thanks.
> >
> > a = (1:5)'
> > b = (6:10)'
> >
> > for n = 1:5
> >     c(n) = [a(n,:);b(n,:)]
> > end
> > c
>
> If for row you can do: c=cat(1,a,b)
> For column you can do as previously advised or c=cat(2,a,b)


Hi -

Thanks your reply.
Actually, I want to save [1 6], [2 7], [3 8], [4 9] and [5 10] at each looping to different file named 1.txt, 2.txt, 3.txt, 4.txt and 5.txt, respectively.

For example:
c(1) = [1 6] --------> then save to 1.txt
c(2) = [2 7] --------> then save to 2.txt
c(3) = [3 8] --------> then save to 3.txt
c(4) = [4 9] --------> then save to 4.txt
c(5) = [5 10] --------> then save to 5.txt

How to do that?

Michael

Subject: How to append a new row of data to the current matrix?

From: ImageAnalyst

Date: 6 Sep, 2011 07:47:56

Message: 6 of 7

On Sep 6, 5:54 am, "Kuo-Hsien" <mchan...@hotmail.com> wrote:
> Anh Nguyen <theanh.ngu...@qut.edu.au> wrote in message <812df0b9-f72a-4b0a-86b5-4ef23adfe...@n19g2000prh.googlegroups.com>...
> > On Sep 6, 12:33 am, "Kuo-Hsien" <mchan...@hotmail.com> wrote:
> > > Dear my friends,
>
> > > I want to keep appending a new row of data to the current matrix. How to do that?
>
> > > Here is my code what I want to do, but it doesn't work. Please advice me. Thanks.
>
> > > a = (1:5)'
> > > b = (6:10)'
>
> > > for n = 1:5
> > >     c(n) = [a(n,:);b(n,:)]
> > > end
> > > c
>
> > If for row you can do: c=cat(1,a,b)
> > For column you can do as previously advised or c=cat(2,a,b)
>
> Hi -
>
> Thanks your reply.
> Actually, I want to save [1 6], [2 7], [3 8], [4 9] and [5 10] at each looping to different file named 1.txt, 2.txt, 3.txt, 4.txt and 5.txt, respectively.
>
> For example:
> c(1) = [1 6]    --------> then save to 1.txt
> c(2) = [2 7]    --------> then save to 2.txt
> c(3) = [3 8]    --------> then save to 3.txt
> c(4) = [4 9]    --------> then save to 4.txt
> c(5) = [5 10]    --------> then save to 5.txt
>
> How to do that?
>
> Michael
-------------------------------------------------------------------------
You're still being unclear. Does the first file always go to 1.txt
even if the numbers in the first number in the row vector is not 1?
For example if
c(1) = [23, 42]
would you write to 1.txt or 23.txt?

And your c would have to be a cell array, not a numerical array since
a numerical array can only hold one numerical element at each
location, not two. Are you sure you want to dive into the
complexities of cell arrays when you're this inexperienced with
MATLAB? Do you even need c at all? Why not just use fopen(),
fprintf(), and fclose() to write the data directly to the file?

Have you seen the FAQ:
http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F

Subject: How to append a new row of data to the current matrix?

From: Kuo-Hsien

Date: 6 Sep, 2011 13:10:29

Message: 7 of 7

Hi,

Thanks for your guidance. That helps. I am wondering if you can guide me more in this situation.

a = [1 2 3;4 5 6;7 8 9];
b = [10 11 12;13 14 15;16 17 18];
for n = 1:3
c(n,:) = [a(n,:),b(n,:)]
end

The error message is "Subscripted assignment dimension mismatch."

I want to c to look like this:
At n = 1, c = [1 2 3;10 11 12] ---> save to 1.txt
At n = 2, c = [4 5 6;13 14 15] ---> save to 2.txt
At n = 2, c = [7 8 9;16 17 18] ---> save to 2.txt

I know how to save the files now, but I confuse about the dimension mismatch.

Thanks.

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