Thread Subject: Issues Writing to File

Subject: Issues Writing to File

From: Ashley

Date: 7 Nov, 2009 13:28:01

Message: 1 of 5

I'm having trouble writing to a file using fprintf. The issues is I need to write lines like:

fprintf(' name='C:/Documents and Settings/ecslogon/Desktop/ME726_Project/Marco Test/Test_Job-3.odb'\n');

This means that I need to print " ' " to the file without ending the fprintf statement. How is this done?

The second issue is I need to write " ( " and " ) " to the file without ending the fprintf (see example below) or causing Matlab to give me an inblance error. How is this done?

fprintf(' INVARIANT, 'Max. Principal'), )), ))\n\n');

Thank you for your time,
Ashley

Subject: Issues Writing to File

From: dpb

Date: 7 Nov, 2009 13:58:45

Message: 2 of 5

Ashley wrote:
> I'm having trouble writing to a file using fprintf. The issues is I
> need to write lines like:
> fprintf(' name='C:/Documents and ...

...

 >> sprintf('%s','It''s an embedded ''.')
ans =
It's an embedded '.
 >>

Double up on the apostrophes is one way; another is to insert the
numeric values w/ strrep in an existing string at a desired location or
use strcat or , to build the string.

 >> pstrphe = double('''')
pstrphe =
     39
 >> char(pstrphe)
ans =
'
 >> s = ['That', char(pstrphe), 's a string.']
s =
That's a string.
 >>

for example. repmat() sometimes comes in handy for repeated strings as
well.

help strfun

to see the full ML repertoire from which to choose...

BTW, I assume you did get the max stress data read correctly?

--

Subject: Issues Writing to File

From: Ashley

Date: 7 Nov, 2009 14:08:01

Message: 3 of 5

dpb <none@non.net> wrote in message <hd3uer$cnn$1@news.eternal-september.org>...
> Ashley wrote:
> > I'm having trouble writing to a file using fprintf. The issues is I
> > need to write lines like:
> > fprintf(' name='C:/Documents and ...
>
> ...
>
> >> sprintf('%s','It''s an embedded ''.')
> ans =
> It's an embedded '.
> >>
>
> Double up on the apostrophes is one way; another is to insert the
> numeric values w/ strrep in an existing string at a desired location or
> use strcat or , to build the string.
>
> >> pstrphe = double('''')
> pstrphe =
> 39
> >> char(pstrphe)
> ans =
> '
> >> s = ['That', char(pstrphe), 's a string.']
> s =
> That's a string.
> >>
>
> for example. repmat() sometimes comes in handy for repeated strings as
> well.
>
> help strfun
>
> to see the full ML repertoire from which to choose...
>
> BTW, I assume you did get the max stress data read correctly?
>
> --

Yes thank you I did get the max stress read correctly although I used a different method. Although what you suggested worked I was worried that it would fail if the max stress was for some reason written on a different line. A friend helped me write the following:

fid=fopen(filename,'r');
eofstat=0;

while (eofstat == 0);
    
   tline = fgetl(fid);
   [a,b] = size(tline);
    
    if b >= 9 & tline(1:9) == ' Maximum';
        find = 1;
        values = sscanf(tline, '%s %f' );
        max = values(8);
        
       break
    end
    eofstat = feof(fid);
end

fclose(fid);

Which works very well and doesn't care which line the max stress is writtne on. Thanks again for all your help.
Ashley

Subject: Issues Writing to File

From: dpb

Date: 7 Nov, 2009 14:24:21

Message: 4 of 5

Ashley wrote:
...
> Yes thank you I did get the max stress read correctly although I used
> a different method. Although what you suggested worked I was
> worried that it would fail if the max stress was for some reason
> written on a different line. ...

No, that was the whole point of the 'Maximum' in the format string in
textread()--that _requires_ that the string be matched followed by the
value so that your certain that it isn't a differing one that will be read.

Otherwise, if there were a malformed file using '%s %f' would possibly
allow you to transparently get a value that wasn't the maximum w/o
knowing about it.

But, it's your data, suit yourself...

--

Subject: Issues Writing to File

From: dpb

Date: 7 Nov, 2009 15:09:53

Message: 5 of 5

dpb wrote:
> Ashley wrote:
> ...
>> Yes thank you I did get the max stress read correctly although I used
>> a different method. Although what you suggested worked I was
>> worried that it would fail if the max stress was for some reason
>> written on a different line. ...
>
> No, that was the whole point of the 'Maximum' in the format string in
> textread()--that _requires_ that the string be matched followed by the
> value so that your certain that it isn't a differing one that will be read.
>
> Otherwise, if there were a malformed file using '%s %f' would possibly
> allow you to transparently get a value that wasn't the maximum w/o
> knowing about it.
>
> But, it's your data, suit yourself...

OH...dawned on me what your concern was -- it, indeed, somebody were to
change the format of number of lines/file ahead of the value not a fubar
in the file itself--yes, the textread() solution does require the format
be consistent whereas as long as they don't introduce a maximum
something else before of your intended one the posted solution will
handle it.

--

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
fprintf Ashley 7 Nov, 2009 08:29:03
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com