一个以上的变量可以参考同一句柄对象。因此,用户与手柄类的实例进行交互方式不同于价值类的实例。了解处理对象的行为如何帮助您确定是实现句柄还是值类。该主题说明了其中一些相互作用。
For more information on handle classes, see处理课程。
Certain kinds of MATLAB®对象是处理。当变量固定句柄时,它实际上保留对对象的引用。
Handle objects enable more than one variable to refer to the same object. Handle-object behavior affects what happens when you copy handle objects and when you pass them to functions.
句柄对象变量的所有副本都涉及相同的基础对象。这种参考行为意味着如果h
identifies a handle object, then,
H2 = H;
Creates another variable,h2
,指的是与h
。
例如,MATLABaudioplayer
功能creates a handle object that contains the audio source data to reproduce a specific sound segment. The variable returned by theaudioplayer
函数可以标识音频数据,并使您能够访问对象功能播放音频。
MATLAB software includes audio data that you can load and use to create anaudioplayer
object. This sample load audio data, creates the audio player, and plays the audio:
loadgongFsygongsound = audioplayer(y,fs);玩(冈索)
假设您复制gongSound
object handle to another variable (Gongsound2
):
Gongsound2= gongSound;
变量gongSound
和Gongsound2
are copies of the same handle and, therefore, refer to the same audio source. Access theaudioplayer
information using either variable.
例如,set the sample rate for the gong audio source by assigning a new value to theSampleRate
财产。首先获得当前的采样率,然后设置新的样本率:
sr = gongsound.mamplate;disp(SR)
8192
gongsound.samplerate = sr*2;
You can useGongsound2
访问相同的音频来源:
disp(gongSound2.SampleRate)
16384
Play the gong sound with the new sample rate:
play(gongSound2)
当您将参数传递给函数时,该函数将从工作区中复制变量,其中将函数调用到函数工作区中的参数变量中。
将非手动变量传递到函数不会影响呼叫者工作区中的原始变量。例如,myFunc
modifies a local variable calledvar
, but when the function ends, the local variablevar
no longer exists:
功能myfunc(var)var = var + 1;end
Define a variable and pass it tomyfunc
:
x = 12; myFunc(x)
的价值x
执行后没有更改myFunc(x)
:
disp(x)
12
ThemyFunc
函数可以返回修改后的值,您可以将其分配给相同的变量名称(x
) or another variable.
功能out = myFunc(var) out = var + 1;end
Modify a value inmyfunc
:
x = 12; x = myFunc(x); disp(x)
13
当参数是句柄变量时,该函数仅复制句柄,而不是该句柄识别的对象。两个处理(原始副本和本地副本)都涉及同一对象。
When the function modifies the data referred to by the object handle, those changes are accessible from the handle variable in the calling workspace without the need to return the modified object.
例如,修改缩影
函数更改audioplayer
sample rate:
功能修改缩影(audioObj,sr) audioObj.SampleRate = sr;end
Create anaudioplayer
对象并将其传递给修改缩影
功能:
loadgongFsygongsound = audioplayer(y,fs);disp(gongsound.mamplate)
8192
修改缩影(gongSound,16384) disp(gongSound.SampleRate)
16384
The修改缩影
功能does not need to return a modifiedgongSound
object becauseaudioplayer
对象是处理对象。
Handle objects are members of thehandle
班级。因此,您始终可以使用isa
功能。isa
returns logical真的
(1
) when testing for a handle variable:
loadgongFsygongsound = audioplayer(y,fs);isa(gongSound,'handle')
To determine if a variable is a valid handle object, useisa
和isvalid
:
ifisa(gongSound,'handle') && isvalid(gongSound)。。。end
删除手柄对象后,引用对象的句柄变量仍然存在。这些变量变得无效,因为他们所指的对象不再存在。打电话删除
on the object removes the object, but does not clear handle variables.
例如,创建一个audioplayer
目的:
loadgongFsygongsound = audioplayer(y,fs);
The output argument,gongSound
,是一个句柄变量。打电话删除
删除s the object along with the audio source information it contains:
删除(gongSound)
However, the handle variable still exists:
disp(gongSound)
handle to deleted audioplayer
Thewhos
command showsgongSound
as anaudioplayer
目的:
whos
Name Size Bytes Class Attributes Fs 1x1 8 double gongSound 1x1 0 audioplayer y 42028x1 336224 double
Note
The value for Bytes returned by thewhos
命令不包括手柄引用的数据,因为许多变量可以引用相同的数据。
The handlegongSound
不再指一个有效的对象,如isvalid
处理方法:
isvalid(gongSound)
ans = logical 0
打电话删除
on a deleted handle does nothing and does not cause an error. You can pass an array containing both valid and invalid handles to删除
。MATLAB删除有效的手柄,但在遇到已经无效的手柄时不会发出错误。
You cannot access properties with the invalid handle variable:
gongSound.SampleRate
Invalid or deleted object.
Functions and methods that access object properties cause an error:
玩(冈索)
Invalid or deleted object.
要删除变量,gongSound
, 利用清除
:
清除gongSoundwhos
名称大小字节类属性FS 1x1 8双Y 42028X1 336224双重