Path: news.mathworks.com!newsfeed.mathworks.com!arclight.uoregon.edu!wn11feed!worldnet.att.net!207.35.177.252!nf3.bellglobal.com!snoopy.risq.qc.ca!charlie.risq.qc.ca!53ab2750!not-for-mail
From: Gerald Pollack <gerald.DOTpollack.@ATmcgill.DOTca>
Subject: Re: writing to a text file
Newsgroups: comp.soft-sys.matlab
Reply-To: gerald.DOTpollack@ATmcgill.DOTca
References: <eeec35e.-1@webx.raydaftYaTP>
Lines: 45
Organization: McGill University
User-Agent: KNode/0.7.6
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7Bit
Message-ID: <MVU8d.38631$0f.522@charlie.risq.qc.ca>
Date: Wed, 06 Oct 2004 16:18:52 GMT
NNTP-Posting-Host: 132.206.226.58
X-Complaints-To: abuse@mcgill.ca
X-Trace: charlie.risq.qc.ca 1097079532 132.206.226.58 (Wed, 06 Oct 2004 12:18:52 EDT)
NNTP-Posting-Date: Wed, 06 Oct 2004 12:18:52 EDT
Xref: news.mathworks.com comp.soft-sys.matlab:233524



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);