EDA365电子论坛网

标题: matlab中的fprintf函数的具体使用方法 [打印本页]

作者: baqiao    时间: 2020-4-24 14:26
标题: matlab中的fprintf函数的具体使用方法
fprintf函数可以将数据按指定格式写入到文本文件中。其调用格式为:
数据的格式化输出:fprintf(fid, format, variables)
  按指定的格式将变量的值输出到屏幕或指定文件
  fid为文件句柄,若缺省,则输出到屏幕
    1 for standard output (the screen) or 2 for standard error. If FID is omitted, output goes to the screen.
  format用来指定数据输出时采用的格式
    %d 整数
    %e 实数:科学计算法形式
    %f 实数:小数形式
    %g 由系统自动选取上述两种格式之一
    %s 输出字符串
fprintf(fid,format,A)+ E- B; i  k, h( }
说明:fid为文件句柄,指定要写入数据的文件,format是用来控制所写数据格式的格式符,与fscanf函数相同,A是用来存放数据的矩阵。
$ t8 G/ P( G# H( p* _) \例6.9 创建一个字符矩阵并存入磁盘,再读出赋值给另一个矩阵。/ c2 M! W4 e* P: }/ s/ I
>> a='string';3 g$ b) a. H2 Z3 n4 W
>> fid=fopen('d:char1.txt','w');; E' e1 R0 R' E' N: o* S0 c1 A
>> fprintf(fid,'%s',a);7 a$ c" Y/ w1 G7 |. k
>> fclose(fid);
  F. I, A# o4 d. i# f>> fid1=fopen('d:char1.txt','rt');
: ?* G% t# B. p>> fid1=fopen('d:char1.txt','rt');
/ @7 W3 K: V1 o7 E( i3 a6 j  g>> b=fscanf(fid1,'%s')6 f5 M* Y* i& _) r
b =3 W. g% v3 \! y& O$ G- V* u
string
matlab读txt文件
fid=fopen('fx.txt','r');
, U( i/ ]( x+ U  i4 m0 O% F. ]%得到文件号
9 j/ f) p0 v, h( x9 c0 f/ C: c[f,count]=fscanf(fid,'%f %f',[12,90]);( M) h3 f: {) ^7 k% _- k
%把文件号1的数据读到f中。其中f是[12 90]的矩阵
4 R& W6 C* c4 p$ O( g( ~# u%这里'%f %f'表示读取数据的形势,他是按原始数据型读出
6 [5 v! l+ c2 s3 F2 kfclose(fid);
, `6 @  z) z9 c+ c0 V%关闭文件
# k5 ^! J7 T4 }; V另外有的txt文件还可以用load来打开6 A. @5 ?# r* j+ h0 {/ p
其语句为# T5 w6 ?( ~9 }3 z  G, x
f=load('fx.txt)

3 @! x; C+ T' _2 c1 w

1 C" T4 Y" S  L& s9 ?' v, A

  m! E, S- U8 j/ m0 h4 f
8 S: |5 B2 q/ ~; S+ k* P% N/ a: [1 e- B

0 H5 l0 z8 P( h( _/ M" [
作者: yin123    时间: 2020-4-24 14:57
matlab中的fprintf函数的具体使用方法




欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/) Powered by Discuz! X3.2