You are on page 1of 4

Write Formatted Data to File

) (fprintf ) (format ) (fid ).(fopen

: ) (sample .
)fid=fopen(filename,w
fprintf(fid,format,list
)of variables
3.2
8.5
10.4

1
2.1
6.1
9.3
this is a test

;>>i=1
)) (Matlab
;]>>A=[2.1,3.2,8.5;6.1,9.3,10.4
(
;>>B=this is a test

;)>>filename=input(Enter file name =,s
) (s )(input
Enter file name = sample
) (w ) (fid )(fopen
) (i integer ) (\n .
) (f fixed point 4 1
)(s string

)(fclose

;)>>fid=fopen(filename,w
;)>>fprintf(fid,%i\n,i
;)>>fprintf(fid,%4.1f\n,A
;)>>fprintf(fid,%s\n,B
;)>>fclose(fid

:
) (type )(sample

>>type sample
1
2.1
3.2
8.5
6.1
9.3
10.4
this is a test

Read Formatted Data from File


)fid=fopen(filename,r
)fscanf(fid,format,size
) (fscanf ) (format ) (fid ) (r
).(fopen

: ) (sample .

) (s )(input

;)>>filename=input(Enter file name =,s


Enter file name = sample

) (r ) (fid )(fopen

;)>>fid=fopen(filename,r

) (i integer ) (\n
1
) (f fixed point ) 3
(2
) (s string inf

)(fclose

;)>>AC=fscanf(fid,%i\n,1
;)]>>AB=fsacnf(fid,%f\n,[3 2
;)>>AD=fscanf(fid,%s\n,inf

;)>>fclose(fid

:
>>AB
AB
)
3 (2

6.1
9.3
10.4

>>AB

transpose
8.5
10.4

AC

AD

2.1
3.2
8.5

3.2
9.3

2.1
6.1

>>K=AC+2

K=3
>>AD
this is a test

You might also like