6. matlab入門——結構體、元胞數組、字符串


1、結構體

(1)使用賦值方法創建結構體

%% 使用賦值方法創建結構體
person(1).name = '張三';
person(1).weight = 66;
person(1).length = 200;
person(1).width = 30;

person(2).name = '李四';
person(2).weight = 70;
person(2).lengh = 100;
person(2).width = 40;

(2)使用struct創建結構體

%% 使用struct創建結構體
person = struct('name','張三','weight',66,'lengh',100,'width',200);

person = struct('name',{'張三','李四'},'weight',{66,70},'lengh',{100,120},'width',{200,150});
person(1)
person(2)

ans = 
  包含以下字段的 struct:
      name: '張三'
    weight: 66
     lengh: 100
     width: 200

ans = 
  包含以下字段的 struct:
      name: '李四'
    weight: 70
     lengh: 120
     width: 150

(3)訪問結構體內部元素

  獲取:結構體名稱.字段名

  賦值:結構體名稱.字段名 = 新值

%% 訪問結構體內部元素
person = struct('name',{'張三','李四'},'weight',{66,70},'lengh',{100,120},'width',{200,150});
person(1).name    % '張三'
person(1).width = 222
person(1) 

%      name: '張三'
%      weight: 66
%      lengh: 100
%      width: 222

2、元胞數組

  元胞數組每個元素為一個元胞,元胞可以是任意類型、任意尺寸的數據。

(1)使用大括號創建元胞數組

  元胞數組名 = {元胞...元胞}

%% 使用大括號創建元胞數組
a = {rand(3,4),zeros(3);ones(4),rand(4,3)};
size(a) %  2  2

(2)使用元胞創建元胞數組

  元胞數組名(指定索引)= {元胞}

%% 使用元胞創建元胞數組
a(1,1) = {rand(2,3)};
a(1,2) = {ones(4)};
a(2,1) = {zeros(6)};
a(2,2) = {rand(6,4)};

(3)由元胞內容創建元胞數組

  元胞數組名{指定索引}= 元胞內容

  【注意】注意區分上面兩個

%% 由元胞內容創建元胞數組
a{1,1} = rand(2,3);
a{1,2} = ones(4);
a{2,1} = zeros(6);
a{2,2} = rand(6,4);

(4)使用celldisp顯示元胞數組

%% 使用celldisp顯示元胞數組
a{1,1} = rand(2,3);
a{1,2} = ones(4);
a{2,1} = zeros(6);
a{2,2} = rand(6,4);
celldisp(a) % 每一個元胞都會具體顯示出來

(5)使用cellplot顯示元胞數組

%% 使用cellplot顯示元胞數組
a(1,1) = {rand(2,3)};
a(1,2) = {ones(4)};
a(2,1) = {zeros(6)};
a(2,2) = {rand(6,4)};
cellplot(a)

(6)使用大括號訪問元胞數組

  元胞數組{下標}

  元胞數組{下標}(下標)

%% 使用大括號訪問元胞數組
a(1,1) = {rand(2,3)};
a(1,2) = {ones(4)};
a(2,1) = {zeros(6)};
a(2,2) = {rand(6,4)};
a{1,1} % 訪問第一個元胞
a{1,1}(1,2) % 訪問第一個元胞中的第1行第2列那個元素
a{1,1}(1,3) = 10 % 修改第一個元胞中第1行第3列的元素為10

(7)使用小括號訪問元胞數組

  元胞數組(下標)

%% 使用小括號訪問元胞數組
a(1,1) = {rand(2,3)};
a(1,2) = {ones(4)};
a(2,1) = {zeros(6)};
a(2,2) = {rand(6,4)};
a(1,1) % 訪問元胞數組的元素,不是元胞的元素
a(1,:) % 訪問元胞數組的一行

3、字符串

(1)使用方括號創建單行字符串

%% 使用方括號創建單行字符串
s1 = 'MATLAB';
s2 = ' is';
s3 = ' a very good';
s4 = ' software';
% s = [s1 s2 s3 s4];
s = [s1,s2,s3,s4];
disp(s)

% MATLAB is a very good software

(2)使用strcat創建單行字符串

  strcat(子串1,子串2,....,子串n)

  使用strcat函數連接字符串時,每個字符串的最右側空格會被忽略。

%% 使用strcat創建單行字符串
s1 = 'MATLAB';
s2 = ' is';
s3 = ' a very good';
s4 = ' software';
s = strcat(s1,s2,s3,s4);
disp(s)

(3)使用中括號構建多行字符串

  每行的長度要求一致,不足空格來補。

%% 使用中括號構建多行字符串
s1 = 'MATLAB      ';
s2 = ' is         ';
s3 = ' a very good';
s4 = ' software   ';
s = [s1;s2;s3;s4];
size(s)

(4)使用strvcat構建多行字符串(過時,盡量不用)

  strvcat(子串1,子串2,...,子串n)——不要求每行的長度一致

  若有一行是空字符串,那么會被忽略掉。

%% 使用strvcat構建多行字符串
s1 = 'MATLAB';
s2 = ' is';
s3 = ' a very good';
s4 = ' software';
s = strvcat(s1,s2,s3,s4);

(5)使用char構建多行字符串

  char(子串1,子串2,...,子串n)

  空字符串不會被忽略掉,會用空格填滿所要求的長度。

%% 使用char構建多行字符串
s1 = 'MATLAB';
s2 = ' is';
s3 = ' a very good';
s4 = ' software';
s = char(s1,s2,s3,s4);
size(s) % 4 12
s

(6)使用函數比較字符串

  strcmp(a,b)——比較字符串a,b是否完全相同

  strcmpi(a,b)——比較字符串a,b在忽略大小寫的情況下是否完全相同

  strncmp(a,b,n)——比較字符串a,b的前n個字符是否完全相同

  strncmpi(a,b,n)——比較字符串a,b的前n個字符在忽略大小寫的情況下是否完全相同

%% 使用函數比較字符串
s1 = 'matlab';
s2 = 'matlab';
s3 = 'matlab is very good';
s4 = 'MATLAB';
s5 = 'MATLAB IS VERY GOOD';
strcmp(s1,s2) % 1
strcmp(s1,s3) % 0
strcmp(s1,s4) % 0
strncmp(s1,s3,6) % 1
strcmpi(s3,s5) % 1
strncmpi(s1,s5,6) % 1

(7)使用等號比較字符串

  ==  比較的每個字符中的元素相比較

%% 使用等號比較字符串
s1 = 'MATLAB';
s2 = 'matlab';
s3 = 'm';
s4 = 'a';
s1 == s2 %  0   0   0   0   0   0
s2 == s3 %  1   0   0   0   0   0
s2 == s4 %  0   1   0   0   1   0

(8)使用strfind查找字符串

  strfind(string,pattern)——查找字符串string中是否包含pattern子串;

  如果包含返回子串位置,否則返回空數組。

%% 使用strfind查找字符串
s = 'matlab is a very good software';
pl = 'matlab';
strfind(s,pl) % 1

(9)使用strrep替換字符串

  sNew = strrep(s,s1,s2)——把字符串s中的s1替換成s2

  要有返回值(新字符串);該函數區分大小寫;有幾個換幾個。

%% 使用strrep替換字符串
s = 'matlab is a very good software';
s1 = 'matlab';
s2 = 'MATLAB';
sNew = strrep(s,s1,s2);
s  % 'matlab is a very good software'
sNew  % 'MATLAB is a very good software'

(10)使用函數int2str將數值數組轉換為字符數組

  s = int2str(n)

%% 使用函數int2str將數值數組轉換為字符數組
x1 = 55;
y1 = int2str(x);
whos % 查看類型、大小

x2 = [1,2,3,4];
y2 = int2str(x);
whos

x3 = 2017;
s = 'matlab';
p = [s int2str(x3)];
disp(p) % matlab2017

(11)使用函數str2num將字符數組轉換為數值數組

  n = str2num(s)

%% 使用函數str2num將字符數組轉換為數值數組
s = '56';
n = str2num(s);
whos
n  % 56


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM