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