No BSD License  

Highlights from
FAPPENDL

3.5

3.5 | 2 ratings Rate this file 1 Download (last 30 days) File Size: 1.58 KB File ID: #18830

FAPPENDL

by Mukhtar Ullah

 

19 Feb 2008 (Updated 19 Feb 2008)

FAPPENDL Write line to file in an intuitive syntax rather than FPRINTF

| Watch this File

File Information
Description

FAPPENDL(FID, ARG1, ARG2, ...) is the same as
 FPRINTF(FID, '%s\n', [ARG1, ' ', ARG2, ' ', ...]) and hence provides an
 intuitive syntax for writing lines to a file. When all you need is write
 a single line to a file, the original syntax provided by FPRINTF becomes
 quite cumbersome. The best way is illustrate by an example.

 First write a file 'fname1.tex' using FPRINTF:

    pgffile = 'drawing.pgf';
    fid = fopen('fname1.tex', 'w');
    fprintf(fid, '%s\n', '\documentclass{article}');
    fprintf(fid, '%s\n', '\usepackage{pgf}');
    fprintf(fid, '%s\n', '\begin{document}');
    fprintf(fid, '%s\n', ['\input{', pgffile, '}']);
    fprintf(fid, '%s\n', 'Sample text');
    fprintf(fid, '%s\n', 'Sample math $\langle X(t)\rangle$');
    fprintf(fid, '%s\n', '\end{document}');
    fclose(fid);

 Then write another file 'fname2.tex' with the same content but using
 FAPPENDL:

    fid = fopen('fname2.tex', 'w');
    fappendl fid \documentclass{article}
    fappendl fid \usepackage{pgf}
    fappendl fid \begin{document}
    fappendl(fid, ['\input{', pgffile, '}'])
    fappendl fid Sample text
    fappendl fid Sample math $\langle X(t)\rangle$
    fappendl fid \end{document}
    fclose(fid);

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
20 Feb 2008 John D'Errico

This is a very simple utility, but it is moderately simpler than the alternative of fprintf when you wish to create a text file.

Note that it ONLY writes text lines. So if you wanted to write out the value of some numeric variable, you will need to use fprintf anyway, or convert your numbers to a string on your own.

The help is mainly an example of use, but it seems to get the point across.

21 Feb 2008 John D'Errico

By the way, why did I rate this submission as only a 4? After all, it seems to work reasonably nicely.

I did so because I had to figure out that it does not work if I wanted to append a line with numeric data, that I had first to convert the numeric data to a string. While this is not incredibly hard to do, I had to figure that out by testing the code. There is no statement in the help that this will not work. Since the help is really only an example of use, it gives you only a hint of what DOES work. You need to infer the rest.

Excellent help would have told me all I needed to know about how to use it, without ever testing the code.

21 Feb 2008 Mukhtar Ullah

John,
Thank you for your comments. I will change the help accordingly. The idea is not to replace fprintf but to make life easier for simpler tasks.

22 Feb 2008 K L

I stick with saveascii.m

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
fprintf file text write line Mukhtar Ullah 22 Oct 2008 09:49:34

Contact us at files@mathworks.com