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 3D matrix misprint?

Subject: fprintf 3D matrix misprint?

From: Marco

Date: 26 May, 2009 09:36:01

Message: 1 of 5

Hi out there,

I am using MatLab's 'fprintf' function to produce a formatted data output on screen.
I a have the following problem and hope some of you might be able to help me:

(a) Imagine having a 3d matrix, say S_11_d_f_t.
   size(S_11_d_f_t)=[30,24,124]
   so there are 124 "layers" piled up whose size is 30 (frequencies, "f") *24 (directions, d).

(b) Imagine each "layer" in S, is a timestep (t). In fact, S is my input matrix
    containing each timestep

(c) Aim: using 'fprintf' produce a graphical output (on screen), so that:
   each "lamina" should be written sequentially, or in MatLab language:
  

              for i=1:124
               fprintf('%12.2f% 12.2f% .......12.2f% 12.2f% 12.2f\n', S_11_d_f_t(:,:,i)

               end
(d) PROBLEM: confonrting output oin screen vs original matrix (i.e. each layer of S_11_d_f_t)
numerical values ARE NOT THE SAME.I thik MatLab is picking WRIONG VALUES OF INPUT MATRIX (s).

I suspect I should give a more "proper" explanation to matlab on how to print columns and lines.

Could anybody give me an example please?

Any input is highly appreciated.

Kind regards
Marco

Subject: fprintf 3D matrix misprint?

From: Marco

Date: 26 May, 2009 12:44:02

Message: 2 of 5

Hmmm...

I could solve it already.
A firend of mine just helped me out.

Thanks anyway

Marco



"Marco " <mark.miani@gmail.com> wrote in message <gvgd61$3bl$1@fred.mathworks.com>...
> Hi out there,
>
> I am using MatLab's 'fprintf' function to produce a formatted data output on screen.
> I a have the following problem and hope some of you might be able to help me:
>
> (a) Imagine having a 3d matrix, say S_11_d_f_t.
> size(S_11_d_f_t)=[30,24,124]
> so there are 124 "layers" piled up whose size is 30 (frequencies, "f") *24 (directions, d).
>
> (b) Imagine each "layer" in S, is a timestep (t). In fact, S is my input matrix
> containing each timestep
>
> (c) Aim: using 'fprintf' produce a graphical output (on screen), so that:
> each "lamina" should be written sequentially, or in MatLab language:
>
>
> for i=1:124
> fprintf('%12.2f% 12.2f% .......12.2f% 12.2f% 12.2f\n', S_11_d_f_t(:,:,i)
>
> end
> (d) PROBLEM: confonrting output oin screen vs original matrix (i.e. each layer of S_11_d_f_t)
> numerical values ARE NOT THE SAME.I thik MatLab is picking WRIONG VALUES OF INPUT MATRIX (s).
>
> I suspect I should give a more "proper" explanation to matlab on how to print columns and lines.
>
> Could anybody give me an example please?
>
> Any input is highly appreciated.
>
> Kind regards
> Marco

Subject: fprintf 3D matrix misprint?

From: Steven Lord

Date: 26 May, 2009 13:35:31

Message: 3 of 5


"Marco " <mark.miani@gmail.com> wrote in message
news:gvgd61$3bl$1@fred.mathworks.com...
> Hi out there,
>
> I am using MatLab's 'fprintf' function to produce a formatted data output
> on screen.
> I a have the following problem and hope some of you might be able to help
> me:
>
> (a) Imagine having a 3d matrix, say S_11_d_f_t.
> size(S_11_d_f_t)=[30,24,124]
> so there are 124 "layers" piled up whose size is 30 (frequencies, "f")
> *24 (directions, d).
>
> (b) Imagine each "layer" in S, is a timestep (t). In fact, S is my input
> matrix
> containing each timestep
>
> (c) Aim: using 'fprintf' produce a graphical output (on screen), so that:
> each "lamina" should be written sequentially, or in MatLab language:
>
>
> for i=1:124
> fprintf('%12.2f% 12.2f% .......12.2f% 12.2f% 12.2f\n',
> S_11_d_f_t(:,:,i)
>
> end
> (d) PROBLEM: confonrting output oin screen vs original matrix (i.e. each
> layer of S_11_d_f_t)
> numerical values ARE NOT THE SAME.I thik MatLab is picking WRIONG VALUES
> OF INPUT MATRIX (s).

Keep in mind that MATLAB matrices are column-major and that FPRINTF is
vectorized down the columns, so this will print S_11_d_f_t(1, 1, i) first,
then element (2, 1, i), then (3, 1, i), etc. I'm guessing you expected it
to print the elements in order (1, 1, i) then (1, 2, i) then (1, 3, i) etc.
If that's correct, in order to do that, you'd need to compute the transpose
of the matrix:

S_11_d_f_t(:, :, i).'

and pass that into FPRINTF. Now rows of S_11_d_f_t are columns of the
matrix being passed into FPRINTF, so it vectorizes the way I think you
expected.

In fact, if you want to print each row of S_11_d_f_t on the first page of
the matrix, then iterate over the pages, you don't need the FOR loop. You
will need to use a generalization of the transpose operator, the PERMUTE
function:

format long g
r = rand(2, 3, 4)
fprintf('%f %f %f\n', permute(r, [2 1 3]))

Or to make it really general, you can specify the format string to be
autogenerated based on the size of r:

fprintf([repmat('%f ', 1, size(r, 2)) '\n'], permute(r, [2 1 3]))

--
Steve Lord
slord@mathworks.com

Subject: fprintf 3D matrix misprint?

From: edward kabanyas

Date: 6 Apr, 2012 08:23:17

Message: 4 of 5

Hi steven,

I follow your code to save 3D matrix:

fileID = fopen('test.txt','w');
format long g
r = rand(2, 3, 4)
fprintf(fileID , '%f %f %f\n', permute(r, [2 1 3]))

However, when I load it again into matlab:
>> load('test.txt');

data become 1x24 array not 4x3x2 as the original. Any suggestion to save 3D matrix to text file ? Thank you..

Edward


"Steven Lord" <slord@mathworks.com> wrote in message <gvgr6m$23e$1@fred.mathworks.com>...
>
> "Marco " <mark.miani@gmail.com> wrote in message
> news:gvgd61$3bl$1@fred.mathworks.com...
> > Hi out there,
> >
> > I am using MatLab's 'fprintf' function to produce a formatted data output
> > on screen.
> > I a have the following problem and hope some of you might be able to help
> > me:
> >
> > (a) Imagine having a 3d matrix, say S_11_d_f_t.
> > size(S_11_d_f_t)=[30,24,124]
> > so there are 124 "layers" piled up whose size is 30 (frequencies, "f")
> > *24 (directions, d).
> >
> > (b) Imagine each "layer" in S, is a timestep (t). In fact, S is my input
> > matrix
> > containing each timestep
> >
> > (c) Aim: using 'fprintf' produce a graphical output (on screen), so that:
> > each "lamina" should be written sequentially, or in MatLab language:
> >
> >
> > for i=1:124
> > fprintf('%12.2f% 12.2f% .......12.2f% 12.2f% 12.2f\n',
> > S_11_d_f_t(:,:,i)
> >
> > end
> > (d) PROBLEM: confonrting output oin screen vs original matrix (i.e. each
> > layer of S_11_d_f_t)
> > numerical values ARE NOT THE SAME.I thik MatLab is picking WRIONG VALUES
> > OF INPUT MATRIX (s).
>
> Keep in mind that MATLAB matrices are column-major and that FPRINTF is
> vectorized down the columns, so this will print S_11_d_f_t(1, 1, i) first,
> then element (2, 1, i), then (3, 1, i), etc. I'm guessing you expected it
> to print the elements in order (1, 1, i) then (1, 2, i) then (1, 3, i) etc.
> If that's correct, in order to do that, you'd need to compute the transpose
> of the matrix:
>
> S_11_d_f_t(:, :, i).'
>
> and pass that into FPRINTF. Now rows of S_11_d_f_t are columns of the
> matrix being passed into FPRINTF, so it vectorizes the way I think you
> expected.
>
> In fact, if you want to print each row of S_11_d_f_t on the first page of
> the matrix, then iterate over the pages, you don't need the FOR loop. You
> will need to use a generalization of the transpose operator, the PERMUTE
> function:
>
> format long g
> r = rand(2, 3, 4)
> fprintf('%f %f %f\n', permute(r, [2 1 3]))
>
> Or to make it really general, you can specify the format string to be
> autogenerated based on the size of r:
>
> fprintf([repmat('%f ', 1, size(r, 2)) '\n'], permute(r, [2 1 3]))
>
> --
> Steve Lord
> slord@mathworks.com
>

Subject: fprintf 3D matrix misprint?

From: Steven_Lord

Date: 6 Apr, 2012 13:27:14

Message: 5 of 5



"edward kabanyas" <djuky_hmi@yahoo.com> wrote in message
news:jlm95l$bp9$1@newscl01ah.mathworks.com...
> Hi steven,
>
> I follow your code to save 3D matrix:
>
> fileID = fopen('test.txt','w');
> format long g
> r = rand(2, 3, 4)
> fprintf(fileID , '%f %f %f\n', permute(r, [2 1 3]))
>
> However, when I load it again into matlab:
>>> load('test.txt');
>
> data become 1x24 array not 4x3x2 as the original. Any suggestion to save
> 3D matrix to text file ? Thank you..

Write the size to the file first. When reading, read the size first then the
data and RESHAPE (permuting if necessary.)

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

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