Solved

MATLAB : using fprintf in a loop

Posted on 2004-10-06
2
1,907 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

821 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