Solved

MATLAB : using fprintf in a loop

Posted on 2004-10-06
2
1,903 Views
Last Modified: 2006-11-17
GOAL: I want to write a loop so that a text file is generated using data that has been stored in a series of systematically named MAT files.

The loop code is here:

openfile = strcat('CEA_costlevel',lev,'.txt');
out1 = fopen(openfile,'w')
createdate = datestr(now);

for run =  1:runs  
            runstr = int2str(run_num);
            printstring = strcat('nh_run =',runstr,'\tDate =',createdate,'\n');
            fprintf(out1, printstring);
            for strat_num = 1:strategies
                strat = int2str(strat_num);
                printstring = strcat('strat\tdis_cost\n');
                fprintf(out1, printstring);
                           
                %PROBLEM IS: HOW TO GENERALIZE THE LINE BELOW?
                fprintf(out1,''%d\t%10.4f\n', strat_num, run_1strat_1.tot_dis_cost);
            end
end


The problem is with the "fprintf" line. the variable names for the float variables are systematically named as follows. run_1strat_1.tot_dis_cost should be printed to the file in the first iteration of the loop, then run_1strat_2.tot_dis_cost in the next iteration, and run1strat_3.tot_dis_cost in the next, and so on till the inner and outer loop are exhausted.

In the other situations where I need to fprintf something, a made a string "printstring" first. But I can't figure out to make the printstring when there are formated float variables that need to be part of the string. I thought typing \' would let me insert a single quote but this doesnt work in MATLAB.

Thanks to anyone who can help me with this.

-_Steve
0
Comment
Question by:steveresch
2 Comments
 
LVL 1

Expert Comment

by:lubovec
ID: 12243570
hi

it will be better use of array of float instead run_1strat_X.tot_dis_cost, isn't it?
0
 
LVL 3

Accepted Solution

by:
PodExpert earned 350 total points
ID: 12245886
Hello Steve,

this can be done using eval function, which is present in almost every interpreted langugage (including Matlab Script). Try the following instead of the fprintf line:

myValue = eval(strcat('run_1strat_', int2str(strat_num), '.tot_dis_cost'));
fprintf(out1,''%d\t%10.4f\n', strat_num, myValue);

Note that eval(s) evaluates a string "s" as it was an ordinary matlab expression and returns its value. You can therefor use the "parametric" variable names you need - first, the variable name is created by concatenating "run_1strat_", integer strat_num converted to a strig, and ".tot_dis_cost". Then we evaluate it, get the result and send it to fprintf.

Good luck.
P.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question