Matlab(三)

Matlab变量类型
Matlab(三)_第1张图片
变量转换

>> a=20;
>> b=int8(a);
>> whos
  Name      Size            Bytes  Class     Attributes

  a         1x1                 8  double              
  b         1x1                 1  int8       

字符与ASCII码之间的转换

>> s='a';
>> uint16(s)

ans =

  uint16

   97

字符串拼接

>> s1='hello';
>> s2='world';
>> s3=[s1 s2]

s3 =

    'helloworld'

>> s3=[s1 ' ' s2]

s3 =

    'hello world'

>> s4=[s1;s2]   %s1、s2字符串长度不等会报错

s4 =

  2×5 char 数组

    'hello'
    'world'

‘==’是逻辑运算

>> str = 'aardvark'; 
'a' == str

ans =

  1×8 logical 数组

   1   1   0   0   0   1   0   0
>> str(str == 'a') = 'Z'

str =

    'ZZrdvZrk'

倒序

>>s1='I like the letter E';
>> s1(end:-1:1)

ans =

    'E rettel eht ekil I'

结构体

>> student.name = 'John Doe'; 
student.id = '[email protected]';
student.number = 301073268; 
student.grade = [100, 75, 73; ...
95, 91, 85.5; ...
100, 98, 72];
student

student = 

  包含以下字段的 struct:

      name: 'John Doe'
        id: '[email protected]'
    number: 301073268
     grade: [3×3 double]
>> student(2).name = 'Ann Lane'; 
student(2).id = '[email protected]';
student(2).number = 301078853;
student(2).grade = [95 100 90; 95 82 97; 100 85 100];
>> student(2)

ans = 

  包含以下字段的 struct:

      name: 'Ann Lane'
        id: '[email protected]'
    number: 301078853
     grade: [3×3 double]

结构体函数
Matlab(三)_第2张图片

>> fieldnames(student)
rmfield(student,'id')

ans =

  4×1 cell 数组

    {'name'  }
    {'id'    }
    {'number'}
    {'grade' }


ans = 

  包含以下字段的 struct:

      name: 'John Doe'
    number: 301073268
     grade: [3×3 double]

嵌套结构体

>> A = struct('data', [3 4 7; 8 0 1], 'nest', ...
struct('testnum', 'Test 1', ...
'xdata', [4 2 8],'ydata', [7 1 6])); 
>> A

A = 

  包含以下字段的 struct:

    data: [2×3 double]
    nest: [1×1 struct]

Cell
两种定义方式

>> A(1,1)={[1 4 3; 0 5 8; 7 2 9]};
A(1,2)={'Anne Smith'};
A(2,1)={3+7i};
A(2,2)={-pi:pi:pi};
A

A =

  2×2 cell 数组

    {3×3 double        }    {'Anne Smith'}
    {[3.0000 + 7.0000i]}    {1×3 double  }
>> A{1,1}=[1 4 3; 0 5 8; 7 2 9];
A{1,2}='Anne Smith';
A{2,1}=3+7i;
A{2,2}=-pi:pi:pi;
A

A =

  2×2 cell 数组

    {3×3 double        }    {'Anne Smith'}
    {[3.0000 + 7.0000i]}    {1×3 double  }
>> A{1,1}

ans =

     1     4     3
     0     5     8
     7     2     9

>> A(1,1)

ans =

  1×1 cell 数组

    {3×3 double}

Matlab(三)_第3张图片
Matlab(三)_第4张图片

>> a = magic(3)
b = num2cell(a)
c = mat2cell(a, [1 1 1], 3)

a =

     8     1     6
     3     5     7
     4     9     2

b =

  3×3 cell 数组

    {[8]}    {[1]}    {[6]}
    {[3]}    {[5]}    {[7]}
    {[4]}    {[9]}    {[2]}

c =

  3×1 cell 数组

    {1×3 double}
    {1×3 double}
    {1×3 double}

多维数组

>> A{1,1,1} = [1 2;4 5]; 
A{1,2,1} = 'Name'; 
A{2,1,1} = 2-4i; 
A{2,1,1} = 7; 
A{1,1,2} = 'Name2'; 
A{1,2,2} = 3; 
A{2,1,2} = 0:1:3; 
A{2,2,2} = [4 5]';
>> A
  2×2×2 cell 数组

A(:,:,1) = 

    {2×2 double}    {'Name'    }
    {[       7]}    {1×3 double}


A(:,:,2) = 

    {'Name2'   }    {[       3]}
    {1×4 double}    {2×1 double}

Matlab(三)_第5张图片
reshape()

>> A = {'James Bond', [1 2;3 4;5 6]; pi, magic(5)}
C = reshape(A,1,4)

A =

  2×2 cell 数组

    {'James Bond'}    {3×2 double}
    {[    3.1416]}    {5×5 double}


C =

  1×4 cell 数组

    {'James Bond'}    {[3.1416]}    {3×2 double}    {5×5 double}

Matlab(三)_第6张图片

>> A=20

A =

    20

>> ischar(A)

ans =

  logical

   0

save() and load()
Matlab(三)_第7张图片
在这里插入图片描述
xlsread() and xlswrite()
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
打开.txt文件
Matlab(三)_第8张图片

你可能感兴趣的:(Matlab,matlab)