Solved

MATLAB : using fprintf in a loop

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 72
scoresClump  challenge 31 108
What is Python programming? 3 68
oracle query help 18 78
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 problems I had with the new location API and permissions in Marshmallow
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…

759 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

17 Experts available now in Live!

Get 1:1 Help Now