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:
fprintf for Matrix

Subject: fprintf for Matrix

From: Elefante

Date: 26 Dec, 2008 07:38:48

Message: 1 of 6


        X=M(1:end,1);

        Y=M(1:end,2);

        Z=M(1:end,3);


        fprintf('%14.3f\n',X);

        fprintf('%14.3f\n',Y);

                fprintf('%14.3f\n',Z);

    The Resulted all right it's O.K.

        Because with:

        fprintf('%14.3f %14.3f %14.3f\n',M);

    or:

                fprintf('%14.3f %14.3f %14.3f\n',X,Y,Z);

    The Resulted it's NO O.K., but different ?

    Thank you.

Subject: fprintf for Matrix

From: Roger Stafford

Date: 26 Dec, 2008 17:45:04

Message: 2 of 6

"Elefante" <elefante@tiscalinet.it> wrote in message <49548a0b$0$1117$4fafbaef@reader3.news.tin.it>...
>
> X=M(1:end,1);
> Y=M(1:end,2);
> Z=M(1:end,3);
>
> fprintf('%14.3f\n',X);
> fprintf('%14.3f\n',Y);
> fprintf('%14.3f\n',Z);
>
> The Resulted all right it's O.K.
> Because with:
>
> fprintf('%14.3f %14.3f %14.3f\n',M);
>
> or:
>
> fprintf('%14.3f %14.3f %14.3f\n',X,Y,Z);
>
> The Resulted it's NO O.K., but different ?
> Thank you.

  Your presentation is not very clear, but I am guessing you want the values of M printed out in an arrangement similar to what you would get with matlab's display of M. If so, you should do it this way:

 fprintf('%14.3f %14.3f %14.3f\n',M(:,1:3).');

  The reason is that 'fprintf' prints elements of matrix M in the order they occur in M(:), that is in matlab's standard "column" order and what you presumably want is "row" order, so a transpose is necessary.

Roger Stafford

Subject: fprintf for Matrix

From: Elefante

Date: 27 Dec, 2008 06:44:25

Message: 3 of 6

        O.K. Thank You
        Bye


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> ha scritto nel
messaggio news:gj3570$4pf$1@fred.mathworks.com...
> "Elefante" <elefante@tiscalinet.it> wrote in message
> <49548a0b$0$1117$4fafbaef@reader3.news.tin.it>...
>>
>> X=M(1:end,1);
>> Y=M(1:end,2);
>> Z=M(1:end,3);
>>
>> fprintf('%14.3f\n',X);
>> fprintf('%14.3f\n',Y);
>> fprintf('%14.3f\n',Z);
>>
>> The Resulted all right it's O.K.
>> Because with:
>>
>> fprintf('%14.3f %14.3f %14.3f\n',M);
>>
>> or:
>>
>> fprintf('%14.3f %14.3f %14.3f\n',X,Y,Z);
>>
>> The Resulted it's NO O.K., but different ?
>> Thank you.
>
> Your presentation is not very clear, but I am guessing you want the
> values of M printed out in an arrangement similar to what you would get
> with matlab's display of M. If so, you should do it this way:
>
> fprintf('%14.3f %14.3f %14.3f\n',M(:,1:3).');
>
> The reason is that 'fprintf' prints elements of matrix M in the order
> they occur in M(:), that is in matlab's standard "column" order and what
> you presumably want is "row" order, so a transpose is necessary.
>
> Roger Stafford
>

Subject: fprintf for Matrix

From: Osias Guevara

Date: 14 Jul, 2013 22:18:10

Message: 4 of 6

"Elefante" <elefante@tiscalinet.it> wrote in message <49548a0b$0$1117$4fafbaef@reader3.news.tin.it>...
>
> X=M(1:end,1);
>
> Y=M(1:end,2);
>
> Z=M(1:end,3);
>
>
> fprintf('%14.3f\n',X);
>
> fprintf('%14.3f\n',Y);
>
> fprintf('%14.3f\n',Z);
>
> The Resulted all right it's O.K.
>
> Because with:
>
> fprintf('%14.3f %14.3f %14.3f\n',M);
>
> or:
>
> fprintf('%14.3f %14.3f %14.3f\n',X,Y,Z);
>
> The Resulted it's NO O.K., but different ?
>
> Thank you.
>
>
>
>
>
Saludos!!, lo que pasa es que tienes que usar la transpuesta osea:
>>fprintf('%14.3f %14.3f %14.3f\n',M'); y listo!!

Subject: fprintf for Matrix

From: dpb

Date: 16 Jul, 2013 13:23:07

Message: 5 of 6

On 7/14/2013 5:18 PM, Osias Guevara wrote:
> "Elefante" <elefante@tiscalinet.it> wrote in message
> <49548a0b$0$1117$4fafbaef@reader3.news.tin.it>...
>>
>> X=M(1:end,1);
>> Y=M(1:end,2);
>> Z=M(1:end,3);
>> fprintf('%14.3f\n',X);
>> fprintf('%14.3f\n',Y);
>> fprintf('%14.3f\n',Z);
>> The Resulted all right it's O.K.
>>
>> Because with:
>>
>> fprintf('%14.3f %14.3f %14.3f\n',M);
>>
>> or:
>>
>> fprintf('%14.3f %14.3f %14.3f\n',X,Y,Z);
>>
>> The Resulted it's NO O.K., but different ?
...

> Saludos!!, lo que pasa es que tienes que usar la transpuesta osea:
>>> fprintf('%14.3f %14.3f %14.3f\n',M'); y listo!!

Two comments...

A) The expression M(1:end,1) can be written more succinctly as simply
M(:,1) which is also easier on the eyes scanning code. Use the m:n form
when something other than the first and last entries are needed. Saves
typing as well as is easier to read code in the end.

B) The repeated number of elements in a formatting string can often be
minimized by the judicious use of repmat()...

fmt=[repmat('%14.3f ',1,3) '\n'];
fprintf(fmt,M');

Also used the fmt string temp variable that can make it simpler to keep
the xprintf argument list straight. NB: that if the trailing space
before the newline is really not wanted simply use the repmat argument
count one less and repeat the last format string eliminating the
trailing space--

fmt=[repmat('%14.3f ',1,2) '%14.3f\n'];

Why, oh why K&R didn't follow the FORTRAN FORMAT specification pattern
that allows for repeat specifier (or even more, why didn't TMW?) where
the field width.prec values _follow_ the field selector character which
then allows the numeric value preceding the letter to be a repetition
value is beyond ken... :(

--

Subject: fprintf for Matrix

From: dpb

Date: 16 Jul, 2013 16:57:58

Message: 6 of 6

On 7/16/2013 8:23 AM, dpb wrote:
> On 7/14/2013 5:18 PM, Osias Guevara wrote:
>> "Elefante" <elefante@tiscalinet.it> wrote in message
>> <49548a0b$0$1117$4fafbaef@reader3.news.tin.it>...
>>>
>>> X=M(1:end,1);
>>> Y=M(1:end,2);
>>> Z=M(1:end,3);
>>> fprintf('%14.3f\n',X);
>>> fprintf('%14.3f\n',Y);
>>> fprintf('%14.3f\n',Z);
>>> The Resulted all right it's O.K.
>>>
>>> Because with:
>>>
>>> fprintf('%14.3f %14.3f %14.3f\n',M);
>>>
>>> or:
>>>
>>> fprintf('%14.3f %14.3f %14.3f\n',X,Y,Z);
>>>
>>> The Resulted it's NO O.K., but different ?
> ...
>
>> Saludos!!, lo que pasa es que tienes que usar la transpuesta osea:
>>>> fprintf('%14.3f %14.3f %14.3f\n',M'); y listo!!
>
> Two comments...
>
> A) The expression M(1:end,1) can be written more succinctly as simply
> M(:,1) which is also easier on the eyes scanning code. Use the m:n form
> when something other than the first and last entries are needed. Saves
> typing as well as is easier to read code in the end.
>
> B) The repeated number of elements in a formatting string can often be
> minimized by the judicious use of repmat()...
>
> fmt=[repmat('%14.3f ',1,3) '\n'];
> fprintf(fmt,M');
>
...

And a third probably the more important is

C) The cause of your initial problem is owing to internal storage order.
  Remember Matlab storage order is column-major a la Fortran, _not_
row-major per C. Try at the command line

A=[1 2 3; 4 5 6; 7 8 9];
sprintf('%d %d %d\n',A)

and

sprintf('%d %d %d\n',A')

to see the difference...

--

Tags for this Thread

No tags are associated with 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