matlab base function strcmp num2str string format

The evolution together -matlab base function calculates format string strcmp num2str

Find useful, welcome to discuss mutual learning together - Follow Me

strcmp

  • String comparison

    grammar

  • tf = strcmp(s1,s2)

    description

  • tf = strcmp (s1, s2) and compare s1 s2, and if identical, Returns 1 (true). Otherwise, strcmp returns 0 (false). If the text size and content of the same, the text is the same. Tf is the result returned logical data type.
  • Input-cell array may be a combination of character vector, the vector character and a character array.

    Examples

    Compare two strings vector

s1 = 'Yes';
s2 = 'No';
tf = strcmp(s1,s2)
tf =

     0

strcmp returns 0 because s1 and s2 are not equal.

Compare two equal character vectors.

s1 = 'Yes';
s2 = 'Yes';
tf = strcmp(s1,s2)
tf =

     1

strcmp returns 1 because s1 and s2 are equal.

Find text in a cell array

s1 = 'upon';
s2 = {'Once','upon';
      'a','time'};
tf = strcmp(s1,s2)
tf =

     0     1
     0     0

数组s2中只出现一次s1,它发生在元素s2(1,2)处

Compare two character vector cell array

  • Compare two character vector cell array each element
s1 = {'Time','flies','when';
      'you''re','having','fun.'};
s2 = {'Time','drags','when';
      'you''re','anxiously','waiting.'};
tf = strcmp(s1,s2)
tf =

     1     0     1
     1     0     0

There are three elements in the equally Examples of s1, s2. These are the standard (1,1) at the "Time", "when" the subscript (3), the subscript (2,1) "you re".

Input parameters

Output parameters


num2str

  • Convert a number to a string

    grammar

  • s = num2str (A) a digital array to a string representation. Output format depending on the original value. num2str figure is used for labeling and titles to FIG.
  • s = num2str (A, precision) Returns a string representation, which contains the largest number of significant digits specified accuracy.
  • s = num2str (A, formatSpec) formatSpec the specified format to all the elements of A.

    Examples

    The default floating-point conversion

  • The pi and return eps floating point value to a string
s = num2str(pi)
s =

3.1416

s = num2str(eps)
s =

2.2204e-16

Specified accuracy

  • Specifies the maximum number of significant digits of floating-point values
A = gallery('normaldata',[2,2],0);
s = num2str(A,3)
s =

-0.433     0.125
 -1.67     0.288

Specified format

  • Specifying width, precision floating-point format, and the array
A = gallery('uniformdata',[2,3],0) * 9999;
s = num2str(A,'%10.5e\n')
s =

9.50034e+03
6.06782e+03
8.91210e+03
2.31115e+03
4.85934e+03
7.62021e+03
  • Format "% 10.5e" print each index value to five decimal format, "\ n" prints a new line character. Printing and only 10 digits

Print format string

  • Format output field, specify formatted character vector comprising operator. formatSpec may also contain plain text and special characters

    Formatting operator

  • Formatting operator percent sign,% and ending with the conversion character. You may also specify an identifier, flag, field width, and subtype accuracy between operators% and the conversion character. (Void space between the operator, the display readability only here).

    转换字符

    此表显示将数字和字符数据转换为文本格式的转换字符。

    可选操作符

  • 可选标识符、标志、字段宽度、精度和子类型操作符进一步定义了输出文本的格式。
    The optional identifier, flags, field width, precision, and subtype operators further define the format of the output text.

    标识符 Identifier

  • 处理函数输入参数的顺序。使用语法n$,其中n表示函数调用中其他输入参数的位置。
  • Example: ('%3\(s %2\)s %1\(s %2\)s','A','B','C') prints input arguments 'A', 'B', 'C' as follows: C B A B.
  • Note: 如果输入参数是数组,则不能使用标识符从该输入参数指定特定的数组元素。

    标志 Flags

    区域宽度 Field Width

  • 要打印的最小字符数。字段宽度操作符可以是一个数字,或者一个星号( * )来引用输入参数。
  • Example: The input arguments ('%12d',intmax) are equivalent to ('% * d', 12, intmax).
  • 函数在值之前用空格填充字段宽度,除非由标记另行指定。

    精度 Precision

    特殊文本即转义字符

    formatSpec还可以在百分号前、%或转换字符后包含附加文本

    注意

  • 数值转换只打印复数的实数部分。
  • 如果指定不适合数据的转换,例如数值的字符转换,MATLAB将覆盖指定的转换,并使用%e。
    示例:'%s'将pi转换为3.141593e+00。
  • 如果将字符转换(%c或%s)应用于整数值,MATLAB将与有效字符代码对应的值转换为字符。
    示例: ' %s' converts [65 66 67] to ABC.
  • 提示num2str不接受formatSpec输入参数中的位置标识符。例如,num2str([14 15],'%2\(X %1\)o])会报错
  • 位置标识符指定格式化操作符处理函数输入参数的顺序,而不是输入数组的元素。当您调用num2str时,只有一个输入参数需要转换数字。
  • 算法num2str从字符串中删除任何前导空格,即使formatSpec包含空格字符标志。例如,num2str(42.67,'% 10.2f')返回一个1×5的字符数组'42.67'

Guess you like

Origin www.cnblogs.com/cloud-ken/p/11266865.html