Thread Subject: Writing text file with double and string on the same row

Subject: Writing text file with double and string on the same row

From: Pete sherer

Date: 17 Jun, 2009 06:18:01

Message: 1 of 5

Are there any way I can resolve this issue?

a = [1 2 3]';
b = {'a','gg','vb'}';
c = [34 44 98]';
fid = fopen( 'tmp.txt', 'wt');
fprintf( fid,'%f %s %f\n', a, b, c);
fclose( fid);

??? Error using ==> fprintf
Function is not defined for 'cell' inputs.

Thank you very much.
Pete

Subject: Writing text file with double and string on the same row

From: Jos

Date: 17 Jun, 2009 08:53:02

Message: 2 of 5

"Pete sherer" <tsh@abg.com> wrote in message <h1a1qp$89c$1@fred.mathworks.com>...
> Are there any way I can resolve this issue?
>
> a = [1 2 3]';
> b = {'a','gg','vb'}';
> c = [34 44 98]';
> fid = fopen( 'tmp.txt', 'wt');
> fprintf( fid,'%f %s %f\n', a, b, c);
> fclose( fid);
>
> ??? Error using ==> fprintf
> Function is not defined for 'cell' inputs.
>
> Thank you very much.
> Pete

A solution would be to simply loop over the elements:

for ii=1:3,
  fprintf( fid,'%f %s %f\n', a(ii), b{ii}, c(ii));
end

which is both readable and fast.

hth
Jos

Subject: Writing text file with double and string on the same row

From: Pete sherer

Date: 17 Jun, 2009 15:21:01

Message: 3 of 5

I was trying to find a way to write it without using loop.
Any hints?

Subject: Writing text file with double and string on the same row

From: us

Date: 17 Jun, 2009 15:36:02

Message: 4 of 5

"Pete sherer" <tsh@abg.com> wrote in message <h1a1qp$89c$1@fred.mathworks.com>...
> Are there any way I can resolve this issue?
>
> a = [1 2 3]';
> b = {'a','gg','vb'}';
> c = [34 44 98]';
> fid = fopen( 'tmp.txt', 'wt');
> fprintf( fid,'%f %s %f\n', a, b, c);
> fclose( fid);
>
> ??? Error using ==> fprintf
> Function is not defined for 'cell' inputs.
>
> Thank you very much.
> Pete

you could take a look at this small FEX utility, which does what you want (and much more)...

http://www.mathworks.com/matlabcentral/fileexchange/23840

us

Subject: Writing text file with double and string on the same row

From: Jos

Date: 17 Jun, 2009 18:44:02

Message: 5 of 5

"Pete sherer" <tsh@abg.com> wrote in message <h1b1kt$od2$1@fred.mathworks.com>...
> I was trying to find a way to write it without using loop.
> Any hints?

Sure, if you insist ...

A = [num2cell(a) b num2cell(c)].' ; % do not forget to transpose ...
fprintf('%f %s %f\n',A{:})

imho this is less readable and prone to errors

Jos

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
reference us 17 Jun, 2009 11:39:10
fex us 17 Jun, 2009 11:39:10
file exchange us 17 Jun, 2009 11:39:10
rssFeed for this Thread

Contact us at files@mathworks.com