Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fprintf column wise

Subject: fprintf column wise

From: Geant Bepi

Date: 24 Jun, 2009 12:16:01

Message: 1 of 6

Hello !

my code generates three arrays of results namely RM, rf and rp
I want to export these results to a single txt file separated by columns.

Matlab prints all results in one single column and I don't know how to separate each result column wise.
This didn't help as well.

fprintf(fid, 'RM = [%6.5f] \n\c',RM)
fprintf(fid,'rf = [%6.5f] \n\c',rf)
fprintf(fid,'rp = [%6.5f] \t\c',rp)

neither this;

fid = fopen('particle_location.txt','wt');
formatSpecifier = repmat('%6.5f\t', 1, size(fid, 3));
formatSpecifier = [formatSpecifier '\n'];
fprintf(fid,'observed particles \n\n')
fprintf(fid, formatSpecifier, RM.')
fprintf(fid, formatSpecifier, rf.')
fprintf(fid, formatSpecifier, rp.')
fclose(fid)

any advise is highly appreciated !

thanks heaps!

Subject: fprintf column wise

From: us

Date: 24 Jun, 2009 12:24:01

Message: 2 of 6

"Geant Bepi" <timothyschru_useonlywhatsbeforeunderscore@yahoo.co.uk> wrote in message <h1t5e0$8gk$1@fred.mathworks.com>...
> Hello !
>
> my code generates three arrays of results namely RM, rf and rp
> I want to export these results to a single txt file separated by columns.
>
> Matlab prints all results in one single column and I don't know how to separate each result column wise.
> This didn't help as well.
>
> fprintf(fid, 'RM = [%6.5f] \n\c',RM)
> fprintf(fid,'rf = [%6.5f] \n\c',rf)
> fprintf(fid,'rp = [%6.5f] \t\c',rp)
>
> neither this;
>
> fid = fopen('particle_location.txt','wt');
> formatSpecifier = repmat('%6.5f\t', 1, size(fid, 3));
> formatSpecifier = [formatSpecifier '\n'];
> fprintf(fid,'observed particles \n\n')
> fprintf(fid, formatSpecifier, RM.')
> fprintf(fid, formatSpecifier, rf.')
> fprintf(fid, formatSpecifier, rp.')
> fclose(fid)
>
> any advise is highly appreciated !
>
> thanks heaps!

this FEX contribution may be helpful...

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

us

Subject: fprintf column wise

From: Geant Bepi

Date: 24 Jun, 2009 13:16:01

Message: 3 of 6

"us " <us@neurol.unizh.ch> wrote in message <h1t5t1$89r$1@fred.mathworks.com>...
> "Geant Bepi" <timothyschru_useonlywhatsbeforeunderscore@yahoo.co.uk> wrote in message <h1t5e0$8gk$1@fred.mathworks.com>...
> > Hello !
> >
> > my code generates three arrays of results namely RM, rf and rp
> > I want to export these results to a single txt file separated by columns.
> >
> > Matlab prints all results in one single column and I don't know how to separate each result column wise.
> > This didn't help as well.
> >
> > fprintf(fid, 'RM = [%6.5f] \n\c',RM)
> > fprintf(fid,'rf = [%6.5f] \n\c',rf)
> > fprintf(fid,'rp = [%6.5f] \t\c',rp)
> >
> > neither this;
> >
> > fid = fopen('particle_location.txt','wt');
> > formatSpecifier = repmat('%6.5f\t', 1, size(fid, 3));
> > formatSpecifier = [formatSpecifier '\n'];
> > fprintf(fid,'observed particles \n\n')
> > fprintf(fid, formatSpecifier, RM.')
> > fprintf(fid, formatSpecifier, rf.')
> > fprintf(fid, formatSpecifier, rp.')
> > fclose(fid)
> >
> > any advise is highly appreciated !
> >
> > thanks heaps!
>
> this FEX contribution may be helpful...
>
> http://www.mathworks.com/matlabcentral/fileexchange/23840
>
> us

I am so dumb with Matlab.
I cant understand how to use the cprintf syntax to get what I wanted.

on the other hand did you understand my question.

appreciate your advise with a nice explanation :)

Subject: fprintf column wise

From: Leslie McBrayer

Date: 24 Jun, 2009 13:46:32

Message: 4 of 6


"Geant Bepi" <timothyschru_useonlywhatsbeforeunderscore@yahoo.co.uk> wrote
in message news:h1t8uh$sr3$1@fred.mathworks.com...
> "us " <us@neurol.unizh.ch> wrote in message
> <h1t5t1$89r$1@fred.mathworks.com>...
>> "Geant Bepi" <timothyschru_useonlywhatsbeforeunderscore@yahoo.co.uk>
>> wrote in message <h1t5e0$8gk$1@fred.mathworks.com>...
>> > Hello !
>> >
>> > my code generates three arrays of results namely RM, rf and rp
>> > I want to export these results to a single txt file separated by
>> > columns.
>> >
>> > Matlab prints all results in one single column and I don't know how to
>> > separate each result column wise.
>> > This didn't help as well.
>> >
>> > fprintf(fid, 'RM = [%6.5f] \n\c',RM)
>> > fprintf(fid,'rf = [%6.5f] \n\c',rf)
>> > fprintf(fid,'rp = [%6.5f] \t\c',rp)
>> >
>> > neither this;
>> >
>> > fid = fopen('particle_location.txt','wt');
>> > formatSpecifier = repmat('%6.5f\t', 1, size(fid, 3));
>> > formatSpecifier = [formatSpecifier '\n'];
>> > fprintf(fid,'observed particles \n\n')
>> > fprintf(fid, formatSpecifier, RM.')
>> > fprintf(fid, formatSpecifier, rf.')
>> > fprintf(fid, formatSpecifier, rp.')
>> > fclose(fid)
>> >
>> > any advise is highly appreciated !
>> >
>> > thanks heaps!
>>
>> this FEX contribution may be helpful...
>>
>> http://www.mathworks.com/matlabcentral/fileexchange/23840
>>
>> us
>
> I am so dumb with Matlab.
> I cant understand how to use the cprintf syntax to get what I wanted.
>
> on the other hand did you understand my question.
>
> appreciate your advise with a nice explanation :)

If you're looking for output in single columns, such as:

   RM rf rp
.xxxxx .xxxxx .xxxxx
.xxxxx .xxxxx .xxxxx
...

I'd suggest concatenating your results into a single matrix. fprintf prints
the values in column order, but you want to specify rows of output, so make
sure that the matrix looks like the transpose of the file you want to print.

For example, if RM, rf, and rp are all row vectors:

results = [RM; rf; rp];
fid = fopen('particle_location.txt','wt');

% headers
fprintf(fid, 'observed particles \n\n');
fprintf(fid, '%6s\t%6s\t%6s\t\n', 'RM', 'rf', 'rp');

% tab-separated columns of results
fprintf(fid, '%6.5f\t%6.5f\t%6.5f\n', results);

fclose(fid);

Subject: fprintf column wise

From: Geant Bepi

Date: 24 Jun, 2009 14:14:02

Message: 5 of 6

"Leslie McBrayer" <lmcbrayer@mathworks.com> wrote in message <h1tanq$15h$1@fred.mathworks.com>...
>
> "Geant Bepi" <timothyschru_useonlywhatsbeforeunderscore@yahoo.co.uk> wrote
> in message news:h1t8uh$sr3$1@fred.mathworks.com...
> > "us " <us@neurol.unizh.ch> wrote in message
> > <h1t5t1$89r$1@fred.mathworks.com>...
> >> "Geant Bepi" <timothyschru_useonlywhatsbeforeunderscore@yahoo.co.uk>
> >> wrote in message <h1t5e0$8gk$1@fred.mathworks.com>...
> >> > Hello !
> >> >
> >> > my code generates three arrays of results namely RM, rf and rp
> >> > I want to export these results to a single txt file separated by
> >> > columns.
> >> >
> >> > Matlab prints all results in one single column and I don't know how to
> >> > separate each result column wise.
> >> > This didn't help as well.
> >> >
> >> > fprintf(fid, 'RM = [%6.5f] \n\c',RM)
> >> > fprintf(fid,'rf = [%6.5f] \n\c',rf)
> >> > fprintf(fid,'rp = [%6.5f] \t\c',rp)
> >> >
> >> > neither this;
> >> >
> >> > fid = fopen('particle_location.txt','wt');
> >> > formatSpecifier = repmat('%6.5f\t', 1, size(fid, 3));
> >> > formatSpecifier = [formatSpecifier '\n'];
> >> > fprintf(fid,'observed particles \n\n')
> >> > fprintf(fid, formatSpecifier, RM.')
> >> > fprintf(fid, formatSpecifier, rf.')
> >> > fprintf(fid, formatSpecifier, rp.')
> >> > fclose(fid)
> >> >
> >> > any advise is highly appreciated !
> >> >
> >> > thanks heaps!
> >>
> >> this FEX contribution may be helpful...
> >>
> >> http://www.mathworks.com/matlabcentral/fileexchange/23840
> >>
> >> us
> >
> > I am so dumb with Matlab.
> > I cant understand how to use the cprintf syntax to get what I wanted.
> >
> > on the other hand did you understand my question.
> >
> > appreciate your advise with a nice explanation :)
>
> If you're looking for output in single columns, such as:
>
> RM rf rp
> .xxxxx .xxxxx .xxxxx
> .xxxxx .xxxxx .xxxxx
> ...
>
> I'd suggest concatenating your results into a single matrix. fprintf prints
> the values in column order, but you want to specify rows of output, so make
> sure that the matrix looks like the transpose of the file you want to print.
>
> For example, if RM, rf, and rp are all row vectors:
>
> results = [RM; rf; rp];
> fid = fopen('particle_location.txt','wt');
>
> % headers
> fprintf(fid, 'observed particles \n\n');
> fprintf(fid, '%6s\t%6s\t%6s\t\n', 'RM', 'rf', 'rp');
>
> % tab-separated columns of results
> fprintf(fid, '%6.5f\t%6.5f\t%6.5f\n', results);
>
> fclose(fid);
>

wow Leslie!! you understood me!!
you are the man !

thanks a lot..

Subject: fprintf column wise

From: Alpana Gowdar

Date: 1 May, 2010 15:55:09

Message: 6 of 6

Hi, I'm trying to print to a text file in one column so I can export this data into labview. But, I had trouble finding the syntax to do this.

Here's the code I have.

% Open a file for writing
fid = fopen('test_002.txt', 'wt');
% print values in one column (now rows)
fprintf(fid, '%f\c', data2);
fclose(fid);

Thanks in advance.

Tags for this Thread

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.

Contact us