How can i write this to a file?

조회 수: 1 (최근 30일)
Gerben Roos
Gerben Roos 2013년 8월 12일
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