How can i write this to a file?

1 次查看(过去 30 天)
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.

采纳的回答

dpb
dpb 2013-8-12
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by