Solved

MATLAB : using fprintf in a loop

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 116
Exe program is not a valid Win 32 application 15 103
split53 challenge 7 77
listing all functions in JavaScript 19 172
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now