在MATLAB中處理物件行為


MATLAB 是一種面向物件的程式設計 (OOP) 語言,允許我們建立物件並定義它們的行為。

  • 在 MATLAB 中,有一種稱為“控制代碼”的資料型別,用於指向物件。MATLAB 中的控制代碼還允許我們將函式作為其他函式的輸入引數傳遞。

  • 在 MATLAB 程式設計中,控制代碼是按引用而不是按值傳遞的。因此,當對物件的屬性進行任何更改時,它將反映在所有引用中。

  • 在 MATLAB 程式中使用控制代碼可以提高記憶體效率,因為它不需要複製物件的全部資料。

總的來說,控制代碼是 MATLAB 程式設計中一種用於儲存物件引用的物件型別。現在,讓我們討論控制代碼物件的特性。

MATLAB 中控制代碼的副本

當我們在 MATLAB 中建立控制代碼的多個副本時,控制代碼的副本也將指向與原始控制代碼相同的物件。

示例

考慮以下 MATLAB 程式碼來理解這種 MATLAB 控制代碼物件的特性。

% MATLAB program to explain copies of handle object
% Define a function
function square = f(a)
    square = a^2;
end

% Create a function handle
fun_handle = @f;

% Display the square of a number using the original function handle (@f)
fprintf('Square using original handle: %d
', fun_handle(5)); % Create a copy of the function handle copy_fh = fun_handle; % Display the square of a number using the copied function handle fprintf('Square using copied handle: %d
', copy_fh(5));

輸出

Square using original handle: 25
Square using copied handle: 25

解釋

在此 MATLAB 程式碼中,我們首先定義了一個函式“f”來計算數字“a”的平方。然後,我們建立一個函式控制代碼“fun_handle”來呼叫該函式並使用此函式控制代碼顯示結果。

之後,我們建立此函式控制代碼的副本作為“copy_fh”,並使用此複製的函式控制代碼顯示結果。

我們可以看到,兩個函式控制代碼生成的輸出相同。因此,這解釋了當使用複製的函式控制代碼時,它也指向與原始函式控制代碼相同的物件。

使用函式修改控制代碼

在 MATLAB 中,我們還可以使用函式更改/修改函式控制代碼。換句話說,我們可以利用函式控制代碼直接更改物件。因此,當我們傳遞一個指向函式的函式控制代碼時,我們在函式內部對物件所做的任何修改也會影響函式外部存在的原始物件。這是因為我們正在處理同一個物件,只是名稱不同。

總的來說,在 MATLAB 中,當我們使用函式控制代碼更改物件時,實際上是在修改此函式控制代碼指向的物件。

示例

讓我們考慮一個例子來理解 MATLAB 中函式控制代碼的這種行為。

% MATLAB code to modify handles using functions
% Define a function
function result = f(x)
    result = (x.^2) / 2;
end

% Create a function handle
fh = @f;

% Specify values for which the sum to be calculated
a = [5, 3];

% Calculate the sum using the function handle
sum_result = sum(fh(a));

% Display the sum result
fprintf('Sum of the values: %f
', sum_result);

輸出

Sum of the values: 17.000000

解釋

此程式碼演示了我們可以輕鬆地使用函式修改函式控制代碼。在這個例子中,我們使用內建函式“sum”修改了函式控制代碼“fh”來計算兩個數字的和。

確定物件是否為控制代碼

在 MATLAB 中,有一個內建函式“isa”,用於確定指定的物件是否為控制代碼。

此函式採用以下語法

isa(object_name, 'handle')

此函式的輸出是一個布林值,即 True 或 False。如果指定的物件是控制代碼,則該函式將返回 True,否則返回 False。

示例

讓我們考慮一個例子來理解 MATLAB 中控制代碼的這種行為

% MATLAB code to check if an object is a handle
% Define a function
function result = f(x)
    result = (x.^2);
end

% Create a function handle
a = @f;

% Create a variable
b = 200;

% Determine if the objects are a handle or not
fprintf('Determining for the object a:');
disp(isa(a, 'handle'));

fprintf('Determining for the object b:');
disp(isa(b, 'handle'));

輸出

Determining for the object a:  1
Determining for the object b:  0

解釋

這裡,物件“a”的值“1”表示“a”是一個函式控制代碼,它指向使用“@f”建立的函式“f”。第二個結果,即物件“b”的值“0”表示“b”不是函式控制代碼,而是一個變數。

這就是我們在 MATLAB 中檢查給定物件是否為控制代碼的方法。

已刪除的控制代碼物件

在 MATLAB 中,如果我們刪除控制代碼指向的物件,那麼指向該物件的控制代碼仍然可能存在於工作區中。但是,此控制代碼變為無效或為空,因為它指向的物件不再存在。

因此,刪除物件會將物件從工作區中刪除,但不會刪除指向該物件的控制代碼。

示例

讓我們考慮一個例子來理解這個概念。

% MATLAB code to delete an object pointed by a handle
% Create axes to plot a graph
a = axes;

% Specify data to plot the graph
x = 0:.5:2;
y = 0: 0.2: 2;

% Plot data on the axes
plot(a, x, y);

% Delete the axes after plotting
delete(a);

% Determine if the handle still exists or not
fprintf('Does the handle 'a' still exist? ');
if exist('a', 'var')
    disp('Yes');
else
    disp('No');
end

輸出

Does the handle 'a' still exist? Yes

解釋

此程式碼首先使用“axes”函式建立一個 axes 物件,然後繪製由“x”和“y”指定的資料。之後,“delete”函式刪除了 axes。接下來,它檢查指向 axes 物件的控制代碼“a”是否存在。在 MATLAB 中,如果物件被刪除,指向它的控制代碼仍然可能存在。因此,此程式碼的輸出將為“是”。

結論

這就是關於 MATLAB 中控制代碼物件行為的全部內容。在本教程中,我們藉助示例程式解釋了控制代碼物件的各種行為。

更新於:2023年9月7日

62 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始
廣告