Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: fprintf incorrectly writing only the first line of the ASCII
Replies: 5   Last Post: Dec 11, 2013 10:30 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Derek Goring

Posts: 3,892
Registered: 12/7/04
Re: fprintf incorrectly writing only the first line of the ASCII
Posted: Dec 11, 2013 6:51 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On Thursday, December 12, 2013 12:27:14 PM UTC+13, Thomas wrote:
> dpb <none@non.net> wrote in message <l8akr0$jn6$1@speranza.aioe.org>...
>

> > On 12/11/2013 3:06 PM, Thomas wrote:
>
> > > I am performing an optimization of two parameters in a calculation that
>
> > > recreates data and compares to experimental data using a chi^2 analysis.
>
> > > The script that takes variable parameters and calculates chi^2 outputs a
>
> > > single value, "res", and takes two variable parameters, variables(1) and
>
> > > variables(2). The algorithm changes these at each iteration, trying to
>
> > > minimize the output of the script ("res"). To log the parameter sets and
>
> > > their corresponding "res", I use fprintf to create an ASCII file that
>
> > > contains three column vectors that are added to at each iteration: the
>
> > > first for the the calculated "res" values, the second and third for
>
> > > variables(1) and variables(2) respectively. The code I use to do this
>
> > > (at the end of the script that takes variables() and calculates res at
>
> > > each iteration) is as follows:
>
> > >
>
> > > vary1=num2cell((variables));
>
> > > res1=num2cell(res);
>
> > > [nrows,ncols]=size(vary1);
>
> > > filename = 'LOG.txt';
>
> > > fid = fopen(filename, 'a');
>
> > > for row=1:nrows,
>
> > > fprintf(fid, '%s %d\n', res1{row,:}); end;
>
> > > for row=1:nrows,
>
> > > fprintf(fid, '%s %d\n', vary1{row,:});
>
> > > end;
>
> > > fclose(fid);
>
> > >
>
> > >
>
> > > Everything works fine except in the first row of the ASCII file where it
>
> > > leaves out variables(1), that is it has only two columns. It then works
>
> > > correctly for the remainder of the rows, printing res, variables(1), and
>
> > > variables(2) for a total of three columns. WHY won't it print the first
>
> > > row properly? I can't use code to load the ASCII into matlab in the
>
> > > future without manually deleting the first row. Anyone know whats wrong
>
> > > here?
>
> >
>
> > Can't tell w/o a sample test case--nothing appears directly wrong but
>
> > there is no _res_ defined in the code snippet you've shown so there's
>
> > nothing to be said other than something isn't as you think on the first
>
> > pass.
>
> >
>
> > --
>
>
>
> The "res" I refer to is actually a pseudo chi^2 and is calculated from:
>
>
>
> res=0;
>
> for ipt=138:411
>
> res=res+((spc(ipt)-y1(ipt))^2);
>
> end
>
>
>
> where "spc" and "y1" are experimental and simulated data respectively. I take only a section of the data (hence 138:411) that contains features that change with changing the parameters. You could test my code using "fminsearch" by optimizing any function of say, variables=[1,1] that creates 512pts, and compares to some arbitrary vector of 512 pts. To test mine as is I would have to email the full scripts and the program my scripts call, which I can do if you're interested.


I can't see how this can possibly work.
You define res as a scalar, but write it out as a matrix.

Assuming res and variables are numerical matrices, why are you converting them to cell arrays?
Why not fprintf on res and variables, specifying whatever the appropriate format is?
Also, if you transpose the matrices, you can eliminate the loops.
fprintf(fid,fmt,res');
fprintf(fid,fmt,variables');



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.