×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

fprintf

fprintf

(OP)
I'm trying to write all of the data in a matrix into a file, and I have to have a header in this file so I'm using fprintf instead of dlmwrite or something like that.

I have a matrix A, and the format I want the file to be in is:

<header>
A(1,1) A(1,2) A(1,3)
A(2,1) A(2,2) A(2,3)
etc, etc.

After I write the header, I could put A in the file by using the command:

fprintf(file, '%g %g %g\n',A);


The problem is, A is actually 707 columns wide and 353 rows long, so it's impractical for me to write '%g' 707 times.  How can I do this?


Thanks

RE: fprintf

You can use this, dlmwrite(filename, M, '-append').
This way you can write your header with a seperate function and then you dlmwrite to drop the whole matrix in underneath it.

you can read more here

Hope this helps.
BsK

RE: fprintf

(OP)
Thanks BsK, you are awesome.  I should have realized you could use dlmwrite even if you had some text, it just seem kinda funny to me that I had to pound everything out with fprintf.

RE: fprintf

(OP)
As a follow up to this I've decided to stick with fprintf.  I used a 'for' loop to create a string that is 707 identifiers long, e.g. '%.16g %.16g %.16g etc. etc.'.  My program took 11 seconds to run when using fprintf.

I figured using dlmwrite and '-append' would have been faster, but it turned out to be about the same speed and then I realized I hadn't specified precision.  When I put 'precision','%.16g' in to my dlmwrite command, the program took 130 seconds to complete!  More than ten times what it did with fprintf.

I would have thought dlmwrite would have been faster, but I'm sticking with fprintf even if it looks kinda ugly.

RE: fprintf

Hoopz
just curious..

What was your delimiter with dlmwrite?


How long did it take for the script to write out the identifiers?


BsK

RE: fprintf

(OP)
The format I needed to put the data in was space-delimited, so my delimiter with dlmwrite was simply a ' '

I just used the code below to create the identifer string; there is probably a better way to do it.  Still, Matlab reported the elapsed time as only 0.122 seconds, although I am on a speedy comp - 3.8 GHz P4, with 2 GB of RAM.

a = '%.16g';
for i=1:706
    a= [a ' %.16g'];
end
a = [a '\n'];

RE: fprintf

How about this

CODE

A = rand(707,353);
a = '%.16g ';
b = repmat(a,1,353);
b = [b '\n'];

f = fopen('test.txt','w');
fprintf(f,'%s\n', 'My header information');
fprintf(f, b, A.');

fclose(f);

M

--
Dr Michael F Platten

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources