Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MATLAB : using fprintf in a loop

Posted on 2004-10-06
2
Medium Priority
?
1,923 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 1400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is about my first experience with programming Arduino.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Starting up a Project
Screencast - Getting to Know the Pipeline

824 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