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 columnmajor 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
>
