Got Questions? Get Answers.
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:
error message on c(n) = cat(1,a(n,:),b(n,:))

Subject: error message on c(n) = cat(1,a(n,:),b(n,:))

From: Kuo-Hsien

Date: 6 Sep, 2011 04:07:10

Message: 1 of 9

Dear all,

I make a loop to pull out the data and save to files, but I got the error messages.

??? In an assignment A(I) = B, the number of elements in B and
 I must be the same.

What's wrong on my code?
a = (1:5)'
b = (5:10)'
for n = 1:5
  c(n) = cat(1,a(n,:),b(n,:))
end

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?

Subject: error message on c(n) = cat(1,a(n,:),b(n,:))

From: vedenev

Date: 6 Sep, 2011 08:43:42

Message: 2 of 9

a = (1:5)' ;
b = (6:10)';
c=zeros(5,2);
delimeter=' ';
for n = 1:5
  c(n,:) = [a(n,:),b(n,:)];
  filename=[num2str(n) '.txt' ];
  dlmwrite(filename, c(n,:), delimeter);
end

-----------------------------------------
Quaternions visualizations in Matlab:
http://quaternion.110mb.com/
vedenev@ngs.ru

Subject: error message on c(n) = cat(1,a(n,:),b(n,:))

From: Kuo-Hsien

Date: 6 Sep, 2011 13:00:32

Message: 3 of 9

vedenev <vedenev.maxim@gmail.com> wrote in message <3521dce9-f7b2-4e11-9e4d-3c0595a78fce@q8g2000yqe.googlegroups.com>...
> a = (1:5)' ;
> b = (6:10)';
> c=zeros(5,2);
> delimeter=' ';
> for n = 1:5
> c(n,:) = [a(n,:),b(n,:)];
> filename=[num2str(n) '.txt' ];
> dlmwrite(filename, c(n,:), delimeter);
> end

Hi vedenev,

Thanks. That is a good lesson. Can you guide me how to take care in the following matrix?!

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

Subject: error message on c(n) = cat(1,a(n,:),b(n,:))

From: Steven_Lord

Date: 6 Sep, 2011 13:47:19

Message: 4 of 9



"Kuo-Hsien" <mchangks@hotmail.com> wrote in message
news:j4469e$ghj$1@newscl01ah.mathworks.com...
> Dear all,
>
> I make a loop to pull out the data and save to files, but I got the error
> messages.
>
> ??? In an assignment A(I) = B, the number of elements in B and
> I must be the same.
>
> What's wrong on my code?
> a = (1:5)'
> b = (5:10)'
> for n = 1:5

During each iteration of this loop, n is a scalar (a 1-by-1.) Therefore, on
the next line:

> c(n) = cat(1,a(n,:),b(n,:))

c(n) is also 1-by-1. However, a(n, :) will be a 1-by-size(a, 2) vector and
b(n, :) will be a 1-by-size(b, 2) vector. That means that the concatenation
will only be defined if a and b have the same number of columns, and the
result will be a 2-by-size(a, 2) matrix. That doesn't "fit" into a 1-by-1
matrix -- it has too many rows.

> 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

a = (1:5)';
b = (5:10)';
for n = 1:numel(a)
    thefile = sprintf('%d.txt', a(n));
    c = [a(n) b(n)];
    % use your favorite file creation function to write c to the file
    % one possibility is SAVE
    save(thefile, 'c', '-ascii')
end

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: error message on c(n) = cat(1,a(n,:),b(n,:))

From: Kuo-Hsien

Date: 6 Sep, 2011 14:04:28

Message: 5 of 9

> > 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
>
> a = (1:5)';
> b = (5:10)';
> for n = 1:numel(a)
> thefile = sprintf('%d.txt', a(n));
> c = [a(n) b(n)];
> % use your favorite file creation function to write c to the file
> % one possibility is SAVE
> save(thefile, 'c', '-ascii')
> end
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Hi Steve,
How to deal with this case? Thanks.
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

Subject: error message on c(n) = cat(1,a(n,:),b(n,:))

From: Steven_Lord

Date: 6 Sep, 2011 15:13:13

Message: 6 of 9



"Kuo-Hsien" <mchangks@hotmail.com> wrote in message
news:j4599c$sn9$1@newscl01ah.mathworks.com...

*snip*

> Hi Steve,
> How to deal with this case? Thanks.
> 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

Please describe in words what you expect from that code; do you want to
write each row of c into a separate file? If so, replace the following in
the code above:

>> c = [a(n) b(n)];

with:

>> c = [a(n, :) b(n, :)];

and that should work.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: error message on c(n) = cat(1,a(n,:),b(n,:))

From: Kuo-Hsien

Date: 6 Sep, 2011 19:03:25

Message: 7 of 9

"Steven_Lord" <slord@mathworks.com> wrote in message <j45da8$dpe$1@newscl01ah.mathworks.com>...
>
>
> "Kuo-Hsien" <mchangks@hotmail.com> wrote in message
> news:j4599c$sn9$1@newscl01ah.mathworks.com...
>
> *snip*
>
> > Hi Steve,
> > How to deal with this case? Thanks.
> > 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
>
> Please describe in words what you expect from that code; do you want to
> write each row of c into a separate file? If so, replace the following in
> the code above:
>
> >> c = [a(n) b(n)];
>
> with:
>
> >> c = [a(n, :) b(n, :)];
>
> and that should work.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Hi Steve,

I want the c looks 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

But, I got the error message, "Subscripted assignment dimension mismatch."
Could you please let me know how to do it and explain why the dimension mismatches??
Thanks.

Subject: error message on c(n) = cat(1,a(n,:),b(n,:))

From: Steven_Lord

Date: 6 Sep, 2011 20:24:24

Message: 8 of 9



"Kuo-Hsien" <mchangks@hotmail.com> wrote in message
news:j45qpt$1tn$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <j45da8$dpe$1@newscl01ah.mathworks.com>...

> Hi Steve,
>
> I want the c looks 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

So you want:

c = [a(n, :); b(n, :)];

instead of:

c = [a(n, :), b(n, :)];

in the loop.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: error message on c(n) = cat(1,a(n,:),b(n,:))

From: Kuo-Hsien

Date: 13 Sep, 2011 01:03:10

Message: 9 of 9

"Steven_Lord" <slord@mathworks.com> wrote in message <j45vhn$i74$1@newscl01ah.mathworks.com>...
>
>
> "Kuo-Hsien" <mchangks@hotmail.com> wrote in message
> news:j45qpt$1tn$1@newscl01ah.mathworks.com...
> > "Steven_Lord" <slord@mathworks.com> wrote in message
> > <j45da8$dpe$1@newscl01ah.mathworks.com>...
>
> > Hi Steve,
> >
> > I want the c looks 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
>
> So you want:
>
> c = [a(n, :); b(n, :)];
>
> instead of:
>
> c = [a(n, :), b(n, :)];
>
> in the loop.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Hi Steve,
How to insert a column into c matrix?

For example:

ADD = [101 102 103; 104 105 106]
c_new = [1 101 2 102 3 103;10 104 11 105 12 106]

Thank you!

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