Thread Subject: writing to a text file

Subject: writing to a text file

From: Alan Wilkes

Date: 6 Oct, 2004 11:27:22

Message: 1 of 4

Hi,
I'm faced with a queer problem..

test is a m X 2 matrix that contains something like this :

511 0
389 375
389 379
385 362
...

fid = fopen('test.txt','w');
fprintf(fid,'%d %d\n',test);
fclose(fid);

But, when I write it to a file, and read it as "type test.txt" as
above.. the matrix is messed up as,
511 389
389 385 ( these r the first column elements in the matrix "test" )
....

It's puzzling why it displays column-major-wise, plus it drops the
zero (0) in row 1 & Can somebody give me the solution to this weird
problem.. ?

Thanks,
Alan

Subject: writing to a text file

From: Waldemar Krzok

Date: 6 Oct, 2004 17:32:46

Message: 2 of 4



Alan Wilkes wrote:
> Hi,
> I'm faced with a queer problem..
>
> test is a m X 2 matrix that contains something like this :
>
> 511 0
> 389 375
> 389 379
> 385 362
> ...
>
> fid = fopen('test.txt','w');
> fprintf(fid,'%d %d\n',test);
> fclose(fid);
>
> But, when I write it to a file, and read it as "type test.txt" as
> above.. the matrix is messed up as,
> 511 389
> 389 385 ( these r the first column elements in the matrix "test" )
> ....
>
> It's puzzling why it displays column-major-wise, plus it drops the
> zero (0) in row 1 & Can somebody give me the solution to this weird
> problem.. ?

try this
fprintf(fid,'%d %d\n',test');

Waldemar

Subject: writing to a text file

From: Gerald Pollack

Date: 6 Oct, 2004 16:18:52

Message: 3 of 4

Alan Wilkes wrote:

> Hi,
> I'm faced with a queer problem..
>
> test is a m X 2 matrix that contains something like this :
>
> 511 0
> 389 375
> 389 379
> 385 362
> ...
>
> fid = fopen('test.txt','w');
> fprintf(fid,'%d %d\n',test);
> fclose(fid);
>
> But, when I write it to a file, and read it as "type test.txt" as
> above.. the matrix is messed up as,
> 511 389
> 389 385 ( these r the first column elements in the matrix "test" )
> ....
>
> It's puzzling why it displays column-major-wise, plus it drops the
> zero (0) in row 1 & Can somebody give me the solution to this weird
> problem.. ?
>
> Thanks,
> Alan
This might be useful:
% write_tab_separated( filename, data )
% data is matrix; columns will be tab-separated in output
function write_tab_separated( filename, data )
        nrows = size(data,1);
        ncols = size(data,2);
        f = fopen( filename, 'w' );
        for i=1:nrows,
                for j=1:ncols-1,
                        fprintf(f, '%d\t',data(i,j) );
                end
                fprintf(f, '%d\n', data(i,ncols) );
        end
        fclose(f);

Subject: writing to a text file

From: Herbert Ramoser

Date: 7 Oct, 2004 08:12:05

Message: 4 of 4

Alan Wilkes wrote:
> Hi,
> I'm faced with a queer problem..
>
> test is a m X 2 matrix that contains something like this :
>
> 511 0
> 389 375
> 389 379
> 385 362
> ....
>
> fid = fopen('test.txt','w');
> fprintf(fid,'%d %d\n',test);
> fclose(fid);
>
> But, when I write it to a file, and read it as "type test.txt" as
> above.. the matrix is messed up as,
> 511 389
> 389 385 ( these r the first column elements in the matrix "test" )
> .....
>
> It's puzzling why it displays column-major-wise, plus it drops the
> zero (0) in row 1 & Can somebody give me the solution to this weird
> problem.. ?

I notice two things in your code:
- open the file in text mode
   fid = fopen('test.txt','wt');
- The data is taken columnwise so this should give you the desired
   output (in Matlab the data is read columnwise):
   fprintf(fid,'%d %d\n', test.');

-Herbert

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com