For Programmers: Free Programming Magazines  


Home > Archive > Matlab > April 2005 > text format?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author text format?
Dan

2005-04-21, 9:00 am

Hi I just want to write a 2d array of integers into a text file of
normal windows numbers with spaces or tabs between the coloumns, and
return characters at the end of each row

I've read all the online help but cant work out which format to use
*grrr*

fileId = fopen('textoutput.txt','wt+')
count = fprintf(fileId,FORMAT,Array)
fclose(fileId)

Someone help me out please :P
thanx
Jérôme

2005-04-21, 9:00 am

Dan wrote:

> I've read all the online help but cant work out which format to use
> *grrr*


Are you sure ?

Anyway :

A=[1 2 3 ; 4 5 6 ; 7 8 9]

f=fopen('textoutput.txt','wt');

fprintf(f,'%d %d %d\n',A');

fclose(f);

Jérôme
Jérôme

2005-04-21, 9:00 am

And now you know how to deal with text format :

try :

A=[1 2 3 ; 4 5 6 ; 7 8 9]
dlmwrite('textoutput.tx',A,' ');

easier, shorter...

or quiet the same :

save('textoutput.tx','A','-ascii');

Jérôme
Dan

2005-04-21, 9:00 am

Jérôme wrote:
>
>
> Dan wrote:
>
> use
>
> Are you sure ?
>
> Anyway :
>
> A=[1 2 3 ; 4 5 6 ; 7 8 9]
>
> f=fopen('textoutput.txt','wt');
>
> fprintf(f,'%d %d %d\n',A');
>
> fclose(f);
>
> Jérôme


Fantastic thank you
Although what if my array has 500 columns, do I have to put 500 lots
of %d in there?
Herbert Ramoser

2005-04-21, 9:00 am

Dan wrote:
> Jérôme wrote:
>
>
>
> Fantastic thank you
> Although what if my array has 500 columns, do I have to put 500 lots
> of %d in there?


Yes, but matlab allows you to generate the format string:

format = [repmat('%d ', 1, 500) '\n'];

-Herbert
Jérôme

2005-04-21, 9:00 am

Same reply :

A=[1 2 3 ; 4 5 6 ; 7 8 9]

n=size(A,2);

f=fopen('t.txt','wt');

fprintf(f,[repmat('%d ',1,n-1) '%d\n'],A');

fclose(f);

This solution avoid blank character at the end of each line due to
'%d '

Jérôme
Jérôme

2005-04-21, 9:00 am

Did you try the solution using dlmwrite ?

I think it's the easier solution in your case.

Jérôme
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com