matlab學習筆記12_4rmfield,arrayfun,structfun,struct2cell,cell2struct


一起來學matlab-matlab學習筆記12

12_4 結構體

rmfield,arrayfun,structfun,struct2cell,cell2struct

覺得有用的話,歡迎一起討論相互學習~

我的微博我的github我的B站

參考文獻
https://ww2.mathworks.cn/help/matlab/ref/rmfield.html
https://ww2.mathworks.cn/help/matlab/ref/arrayfun.html
https://ww2.mathworks.cn/help/matlab/ref/structfun.html
https://ww2.mathworks.cn/help/matlab/ref/struct2cell.html
https://ww2.mathworks.cn/help/matlab/ref/cell2struct.html

refield

  • 刪除結構體中的字段
  • s = rmfield(s,field)
  • s = rmfield(s,field) 從結構體數組 s 中刪除指定的一個或多個字段。使用字符向量元胞數組或字符串數組指定多個字段。s 的維度保持不變。
定義一個包含 first、second、third 和 fourth 字段的標量結構體。

S.first = 1;
S.second = 2;
S.third = 3;
S.fourth = 4;
刪除字段 first 和 fourth。

fields = {'first','fourth'};
S = rmfield(S,fields)
S = struct with fields:
    second: 2
     third: 3

arrayfun

  • 將函數應用於每個數組元素,區別在於structfun 的輸入參數必須是標量結構體。

語法

  • B = arrayfun(func,A)
    • B = arrayfun(func,A) 將函數 func 應用於 A 的元素,一次一個元素。然后 arrayfun 將 func 的輸出串聯成輸出數組 B,因此,對於 A 的第 i 個元素來說,B(i) = func(A(i))。輸入參數 func 是一個函數的函數句柄,此函數接受一個輸入參數並返回一個標量。func 的輸出可以是任何數據類型,只要該類型的對象可以串聯即可。數組 A 和 B 必須具有相同的大小。
      您不能指定 arrayfun 計算 B 的各元素的順序,也不能指望它們按任何特定的順序完成計算。
創建一個非標量結構體數組。每個結構體有一個包含隨機數向量的字段。這些向量具有不同的大小。

S(1).f1 = rand(1,5);
S(2).f1 = rand(1,10);
S(3).f1 = rand(1,15)
S = 1x3 struct array with fields:
    f1

使用 arrayfun 函數計算 S 中每個字段的均值。不能使用 structfun 完成此計算,因為 structfun 的輸入參數必須是標量結構體。

A = arrayfun(@(x) mean(x.f1),S)
A = 1×3

    0.6786    0.6216    0.6069
  • B = arrayfun(func,A1,...,An)
    • B = arrayfun(func,A1,...,An) 將 func 應用於數組 A1,...,An 的元素,因此 B(i) = func(A1(i),...,An(i))。函數 func 必須接受 n 個輸入參數並返回一個標量。數組 A1,...,An 的大小必須全部相同。
創建一個結構體數組,其中每個結構體有兩個包含數值數組的字段。

S(1).X = 5:5:100; S(1).Y = rand(1,20);
S(2).X = 10:10:100; S(2).Y = rand(1,10);
S(3).X = 20:20:100; S(3).Y = rand(1,5)
S = 1x3 struct array with fields:
    X
    Y

繪制數值數組。從 plot 函數返回一個圖形線條對象的數組,並使用這些對象為每一組數據點添加不同的標記。arrayfun 可以返回任何數據類型的數組,只要該數據類型的對象可以串聯即可。

figure
hold on
p = arrayfun(@(a) plot(a.X,a.Y),S);
p(1).Marker = 'o';
p(2).Marker = '+';
p(3).Marker = 's';
hold off

在這里插入圖片描述

  • B = arrayfun( ___ ,Name,Value)
    • B = arrayfun( ___ ,Name,Value) 應用 func 並使用一個或多個 Name,Value 對組參數指定其他選項。例如, 要以元胞數組形式返回輸出值,請指定 'UniformOutput',false。 當 func 返回的值不能串聯成數組時,可以按元胞數組的形式返回 B。可以將 Name,Value 對組參數與上述任何語法中的輸入參數結合使用。
    創建一個非標量結構體數組。每個結構體有一個包含數值矩陣的字段。
    
    

S(1).f1 = rand(3,5);
S(2).f1 = rand(6,10);
S(3).f1 = rand(4,2)
S = 1x3 struct array with fields:
f1

使用 arrayfun 函數計算 S 中每個字段的均值。mean 返回包含每列均值的向量,因此不能以數組的形式返回均值。要以元胞數組的形式返回均值,請指定 'UniformOutput',false 名稱-值對組。

A = arrayfun(@(x) mean(x.f1),S,'UniformOutput',false)
A = 1x3 cell array
{1x5 double} {1x10 double} {1x2 double}


* [B1,...,Bm] = arrayfun( ___ )
* 當 func 返回 m 個輸出值時,[B1,...,Bm] = arrayfun( ___ ) 返回多個輸出數組 B1,...,Bm。func 可以返回不同數據類型的輸出參數,但每一次調用 func 時返回的每個輸出的數據類型必須相同。可以將此語法與前面語法中的任何輸入參數結合使用。
* 從 func 返回的輸出參數的數量可以不同於 A1,...,An 指定的輸入參數的數量。

創建一個非標量結構體數組。

S(1).f1 = 1:10;
S(2).f1 = [2; 4; 6];
S(3).f1 = []
S = 1x3 struct array with fields:
f1

使用 arrayfun 函數計算 S 中每個字段的大小。行數和列數分別輸出在兩個 1×3 數值數組中。

[nrows,ncols] = arrayfun(@(x) size(x.f1),S)
nrows = 1×3

 1     3     0

ncols = 1×3

10     1     0

---

# <font color=Red>structfun</font>
* 對標量結構體的每個字段應用函數--**和arrayfun不同,arrayfun對所有元素應用函數,structfun對所有字段應用函數**

* A = structfun(func,S)
  * A = structfun(func,S) 將函數 func 應用於標量結構體 S 的每個字段,每次一個字段。然后 structfun 將 func 的輸出串聯成列向量 A。輸入參數 func 是一個函數的函數句柄,此函數接受一個輸入參數並返回一個標量。func 的輸出可以是任何數據類型,只要該類型的對象可以串聯即可。A 中的元素數等於 S 中的字段數。

創建一個標量結構體,其字段中包含不同大小的數值數組。

S.f1 = 1:10;
S.f2 = [2; 4; 6];
S.f3 = []
S = struct with fields:
f1: [1 2 3 4 5 6 7 8 9 10]
f2: [3x1 double]
f3: []

計算每個數值數組的均值,然后以數組的形式返回這些均值。

A = structfun(@mean,S)
A = 3×1

5.5000
4.0000
NaN

* A = structfun(func,S,Name,Value)
  * A = structfun(func,S,Name,Value) 應用 func 並使用一個或多個 Name,Value 對組參數指定其他選項。例如,要以結構體形式返回輸出值,**請指定 'UniformOutput',false。** 當 func 返回的值不能合並為數組時,可以按結構體形式返回 A。返回的結構體具有與 S 相同的字段。

創建一個標量結構體,其字段中包含矩陣。

S.f1 = 1:10;
S.f2 = [2 3; 4 5; 6 7];
S.f3 = rand(4,4)
S = struct with fields:
f1: [1 2 3 4 5 6 7 8 9 10]
f2: [3x2 double]
f3: [4x4 double]

計算每個矩陣的均值。mean 返回包含每列均值的向量,因此不能以數組的形式返回均值。要以結構體形式返回均值,請指定 'UniformOutput',false 名稱-值對組。

A = structfun(@mean,S,'UniformOutput',false)
A = struct with fields:
f1: 5.5000
f2: [4 5]
f3: [0.6902 0.3888 0.7627 0.5962]


* [A1,...,Am] = structfun( ___ )
  * 當 func 返回 m 個輸出值時,[A1,...,Am] = structfun(_ __ ) 返回多個輸出數組 A1,...,Am。func 可以返回不同數據類型的輸出參數,但每次調用 func 時返回的每個輸出的數據類型必須相同。可以將此語法與前面語法中的任何輸入參數結合使用。

創建一個標量結構體。

S.f1 = 1:10;
S.f2 = [2 3; 4 5; 6 7];
S.f3 = rand(4,4)
S = struct with fields:
f1: [1 2 3 4 5 6 7 8 9 10]
f2: [3x2 double]
f3: [4x4 double]

計算 S 中每個數組的大小。行數和列數都是一個 3×1 數值數組。

[nrows,ncols] = structfun(@size,S)
nrows = 3×1

 1
 3
 4

ncols = 3×1

10
 2
 4

---

# <font color=Red>struct2cell</font>
* 將結構體轉換為元胞數組
* C = struct2cell(S)
* C = struct2cell(S) 將結構體轉換為元胞數組。元胞數組 C 包含從 S 的字段復制的值。
* struct2cell 函數不返回字段名稱。要返回元胞數組中的字段名稱,請使用 fieldnames 函數。

```matlab
創建一個結構體。

S.x = linspace(0,2*pi);
S.y = sin(S.x);
S.title = 'y = sin(x)'
S = struct with fields:
        x: [1x100 double]
        y: [1x100 double]
    title: 'y = sin(x)'

將 S 轉換為元胞數組。

C = struct2cell(S)
C = 3x1 cell array
    {1x100 double}
    {1x100 double}
    {'y = sin(x)'}

元胞數組不包含字段名稱。要返回元胞數組中的字段名稱,請使用 fieldnames 函數。fieldnames 和 struct2cell 以相同的順序返回字段名稱和值。

fields = fieldnames(S)
fields = 3x1 cell array
    {'x'    }
    {'y'    }
    {'title'}


cell2struct

  • 將元胞數組轉換為結構體數組
  • structArray = cell2struct(cellArray, fields, dim)
  • structArray = cell2struct(cellArray, fields, dim) 通過元胞數組 cellArray 中包含的信息創建一個結構體數組 structArray。
  • fields 參數指定結構體數組的字段名稱。此參數是一個字符數組、字符向量元胞數組或字符串數組。
  • dim 參數向 MATLAB® 指示創建結構體數組時要使用的元胞數組的軸。使用數值 double 指定 dim。
  • 要使用從元胞數組的 N 行中獲取的字段創建一個結構體數組,請在 fields 參數中指定 N 個字段名稱,在 dim 參數中指定數字 1。要使用從元胞數組的 M 列中獲取的字段創建一個結構體數組,請在 fields 參數中指定 M 個字段名稱,在 dim 參數中指定數字 2。
  • structArray 輸出是具有 N 個字段的結構體數組,其中 N 等於 fields 輸入參數中的字段數。生成的結構體中的字段數必須等於沿要轉換的維度 dim 的元胞數。

示例

  • 創建下表以用於此部分中的示例。表中列出了有關一個小型工程公司的員工的信息。按行讀取該表將顯示按部門列出的員工姓名。按列讀取該表將顯示每個員工已在該公司工作的年數。
    在這里插入圖片描述
輸入以下命令以創建初始元胞數組 employees:

devel = {{'Lee','Reed','Hill'}, {'Dean','Frye'}, ...
   {'Lane','Fox','King'}};
sales = {{'Howe','Burns'}, {'Kirby','Ford'}, {'Hall'}};
mgmt = {{'Price'}, {'Clark','Shea'}, {'Sims'}};
qual = {{'Bates','Gray'}, {'Nash'}, {'Kay','Chase'}};
docu = {{'Lloyd','Young'}, {'Ryan','Hart','Roy'}, {'Marsh'}};

employees = [devel; sales; mgmt; qual; docu]
employees =

    {1x3 cell}    {1x2 cell}    {1x3 cell}
    {1x2 cell}    {1x2 cell}    {1x1 cell}
    {1x1 cell}    {1x2 cell}    {1x1 cell}
    {1x2 cell}    {1x1 cell}    {1x2 cell}
    {1x2 cell}    {1x3 cell}    {1x1 cell}

在這里插入圖片描述

將元胞數組轉換為沿維度1的結構體

  1. 轉換沿其第一個維度的 5×3 元胞數組以構造一個具有 5 個字段的 3×1 結構體。沿元胞數組的維度 1 的每一行將變為結構體數組中的一個字段:
    遍歷第一個維度(即垂直維度),包含 5 行,每行的標題如下:

rowHeadings = {'development', 'sales', 'management', 'quality', 'documentation'};
在這里插入圖片描述
2. 將元胞數組轉換為與此維度相關的結構體數組 depts:

depts = cell2struct(employees, rowHeadings, 1)
depts =
3x1 struct array with fields:
    development
    sales
    management
    quality
    documentation
  1. 使用此面向行的結構體查找已在公司工作超過 10 年的開發員工的姓名:
depts(1:2).development
ans =
    'Lee'    'Reed'    'Hill'
ans =
    'Dean'    'Frye'

將相同的元胞數組轉換為沿維度 2 的結構體

  1. 轉換沿其第二個維度的 5×3 元胞數組以構造一個具有 3 個字段的 5×1 結構體。沿元胞數組的維度 2 的每一列將變為結構體數組中的一個字段:
    在這里插入圖片描述
  2. 沿第二個維度(或水平維度)遍歷元胞數組。列標題將變為生成的結構體的字段:
colHeadings = {'fiveYears' 'tenYears' 'fifteenYears'};

years = cell2struct(employees, colHeadings, 2)
years =
5x1 struct array with fields:
    fiveYears
    tenYears
    fifteenYears
  1. 使用列向結構體時,將顯示已在公司工作至少 5 年的銷售和文件部門的員工數。
[~, sales_5years, ~, ~, docu_5years] = years.fiveYears
sales_5years =
    'Howe'    'Burns'
docu_5years =
    'Lloyd'    'Young'

僅將元胞數組的一部分轉換為結構體:

  1. 僅轉換元胞數組的第一行和最后一行。這將生成一個具有 2 個字段的 3×1 結構體數組:
rowHeadings = {'development', 'documentation'};

depts = cell2struct(employees([1,5],:), rowHeadings, 1)
depts =
3x1 struct array with fields:
    development
    documentation

在這里插入圖片描述
2. 顯示對於所有三個時間段屬於這些部門的員工:

for k=1:3
   depts(k,:)
end

ans =
      development: {'Lee'  'Reed'  'Hill'}
    documentation: {'Lloyd'  'Young'}
ans =
      development: {'Dean'  'Frye'}
    documentation: {'Ryan'  'Hart'  'Roy'}
ans =
      development: {'Lane'  'Fox'  'King'}
    documentation: {'Marsh'}


免責聲明!

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



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