.


:




:

































 

 

 

 


-




2.32.1. (sprintf, sscanf).

- MATLAB C, . , , , .

(a1, a2, ) fmt s sprintf:

[s,s_err]=sprintf(fmt,a1,a2,...)

sprintf , s_err. , ,

[s]=sprintf(fmt,a1,a2,...)

, , .

fmt , , % (), , . , , :

%[][][.][{b,t}]letter

(%) (letter); (-,+,, #, 0), - () ( ), + () ( ), () ( ), # , , , ( , f, e E, ( , ); , g G, ); 0 ; , , (), ; , , ( ), ; letter, , ( 2.32.1); b t, - IEEE.

2.32.1. -.

%c
%d
%e ( e)
%E ( E)
%f
%g
%G
%o
%s
%u
%x a - f.
%X A - F.

%d, %u, %o, %x %X (, , double), , MATLAB .

, fmt , , . , 2.31.2, C.

2.32.2. .

\b (Backspace)
\f (Form feed)
\n (New line)
\r (Carriage return)
\t (Horizontal tab)
\\ (Backslash)
\'' (')
%% (%)

MATLAB.

>> sprintf('%-7d %+7d %07d',12345,12345,12345)

ans =

12345 +12345 0012345

>> sprintf('% d',12345)

ans =

>> sprintf('% d',-12345)

ans =

-12345

>> sprintf('%10.6f',pi)

ans =

3.141593

>> sprintf('%9.5f',pi)

ans =

3.14159

>> sprintf('%d',pi)

ans =

3.141593e+00

>> sprintf(' =%10.8f',pi)

ans =

=3.14159265

s, sprintf, sscanf:

A=sscanf(s,fmt)

fmt. , , , , , (, , ..), .

:

, *, s;

%hd, %ld %lg,

(%hd), (%ld) (%lg);

%[s1s2...], s, ;

%[^s1s2...], s, .

:

>> s='2.7183 3.1416';

>> A=sscanf(s,'%f')

A =

2.7183

3.1416

, A :

>> s1=sprintf('%f ',A)

s1 =

2.718300 3.141600

. , .

>> s1=sprintf('%f',A)

s1 =

2.7183003.141600

, , , :

>> s1=sprintf('%-8.4f',A)

s1 =

2.7183 3.1416

sscanf , :

A=sscanf(s,fmt,size)

[A,count,s_err,nextindex]=sscanf(s,fmt)

sprintf sscanf fprintf fscanf, .

2.32.2. (fprintf, fscanf).

, , , , () :

, ;

, , .

, . , fprintf.

:

>> f_id=fopen('sample.dat','wt'); %

>> x=fprintf(f_id,'%s\n','qwerty'); %

>> fclose(f_id);

%. '%s', , 'qwerty'. '\n', . fprintf . , -1, , .. :

>> fprintf(1,'%s\n','qwerty')

qwerty

:

>> fprintf('%s\n','qwerty')

qwerty

fprintf , , , . - . , , . . , MATLAB . , , . , , :

>> fprintf('%5d',1,2,3,4)

1 2 3 4

'%5d' . , :

>> fprintf('%d %f %e',4,7)

4 7.000000

, ,

fprintf. , , x, y z, :

>> x=123;

>> y=2015;

>> z=2.34567;

>> fprintf('%5d %d %8.3f\n',x,y,z);

123 2015 2.346

x ( d ) 5 . , , . y %d 4 . , . 8 ( f ), ( ).

, , , fprintf, MATLAB C++. ( ).

'%', , (w) (.n). , 2.32.3. , (%d, %i, %u, %x, %X, %o) , . , (fix, round, floor ceil).

2.32.3. , .

d
i
o
u
x a, b, c,..., f
X A, B, C,..., F
f
( )
E ( E)
g , ( )
G , ( )
s .
c

, '%', :

'-' , ( ; );

'+' ( );

'' ( );

'#' .

w, , , . 0, , .

n, , , . , C++ ( , n , , (, '10.5s' 5 , , 10 ) .

('\n') , Esc-. 2.32.4.

2.32.4. Esc-.

\b (Backspace)
\f (Form feed)
\n (New line)
\r (Carriage return)
\t (Horizontal tab)

, :

% ;

' ;

\ Esc-.

( ) , :

%% ;

\\ ;

'' ;

\ .

, , fprintf , .

fscanf, :

[a n]=fscanf(f_id,'format',m);

a , ; n ; f_id ; format , ; m .

, a, f_id format. Inf . m, - m1. , [p q], pq.

, fscanf, fprintf (. 2.32.3). , , , , . %f, %e %g , double. %d %i , double.

'%*', , , ( ).

'%s' -. , , , , . '%s', . , , , .

 





:


: 2016-12-06; !; : 743 |


:

:

80% - .
==> ...

1515 - | 1360 -


© 2015-2024 lektsii.org - -

: 0.049 .