How can i write this to a file?

1 visualizzazione (ultimi 30 giorni)
Gerben Roos
Gerben Roos il 12 Ago 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.

Risposta accettata

dpb
dpb il 12 Ago 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.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by