How can i write this to a file?

1 view (last 30 days)
Gerben Roos
Gerben Roos on 12 Aug 2013
I have a variable y which i want to write to a text file in the following way:
RouteOrder('1','1','1','1'):=y(1,1,1,1);
RouteOrder('1','1','1','2'):=y(1,1,1,2);
etc
I dont know how to use the ' as output in the file, therefore i replaced them with a's so i can later replace all the a's with '
I currently have
fid=fopen('y.txt')
fs='OrderRoute(a%da,a%da,a%da,a%da):=%d'
i loop j loop k loop l loop
a=[i,j,k,l,y(i,j,k,l)]
fprintf(fid,fs,a,'\n')
Which results in a file that has only one line just like using (fid,fs,'/n'a
Using fprintf(fid,'\n',fs,a) instead results in an empty file with the right amount of lines.

Accepted Answer

dpb
dpb on 12 Aug 2013
You have to double-up the tic marks to include them. It's in the details of the doc for the various xprintf() functions...
fmt=['RouteOrder(' repmat('''%d'',',1,4) '):=%f\n']
fmt =
RouteOrder('%d','%d','%d','%d',):=%f\n
>> sprintf(fmt,1,1,1,1,.5)
ans =
RouteOrder('1','1','1','1',):=0.500000
>>
Salt the formatting to suit; you may want to add a field width for the subscripts if you want the output aligned neatly w/ increasing subscript values and the precision of the output value of course.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!