グラフィックス オブジェクトの変更
この例では、MATLAB® でグラフィックス オブジェクトを作成、表示、変更する方法を示します。
グラフィックス オブジェクト
MATLAB でプロットを作成すると、一連のグラフィックス オブジェクトが作成されます。figure、axes、line、patch、text などがグラフィックス オブジェクトの例です。次の Figure には、axes、line、text の 3 つのグラフィックス オブジェクトがあります。オプションの出力引数を使用して、作成されたグラフィックス オブジェクトを保存します。
x = -pi:pi/20:pi;
y = sin(x);
f = figure;
p = plot(x,y);
txt1 = text(0.2,0,'sin(x)');
すべてのグラフィックス オブジェクトには、表示および変更可能なプロパティがあります。こうしたプロパティには既定値があります。line オブジェクト p を表示すると、Color、LineStyle、LineWidth など、最もよく使用されるライン プロパティが示されます。
p
p =
Line with properties:
Color: [0.0660 0.4430 0.7450]
LineStyle: '-'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [-3.1416 -2.9845 -2.8274 -2.6704 -2.5133 -2.3562 -2.1991 -2.0420 -1.8850 -1.7279 -1.5708 -1.4137 -1.2566 -1.0996 -0.9425 -0.7854 -0.6283 -0.4712 -0.3142 -0.1571 0 0.1571 0.3142 0.4712 0.6283 0.7854 0.9425 1.0996 … ] (1×41 double)
YData: [-1.2246e-16 -0.1564 -0.3090 -0.4540 -0.5878 -0.7071 -0.8090 -0.8910 -0.9511 -0.9877 -1 -0.9877 -0.9511 -0.8910 -0.8090 -0.7071 -0.5878 -0.4540 -0.3090 -0.1564 0 0.1564 0.3090 0.4540 0.5878 0.7071 0.8090 0.8910 … ] (1×41 double)
Show all properties
MATLAB では、オブジェクトを作成するコマンドにセミコロンがない場合も同様に表示されます。
txt2 = text(x(end), y(end), 'pi')
txt2 =
Text (pi) with properties:
String: 'pi'
FontSize: 10
FontWeight: 'normal'
FontName: 'Helvetica'
Color: [0.1294 0.1294 0.1294]
HorizontalAlignment: 'left'
Position: [3.1416 1.2246e-16 0]
Units: 'data'
Show all properties
グラフィックス オブジェクト プロパティの取得
グラフィックス オブジェクトの個々のプロパティにアクセスするには、ドット表記構文 object.PropertyName を使用します。たとえば、line オブジェクトの LineWidth プロパティを返します。
pcol = p.LineWidth
pcol = 0.5000
ラインの色を赤に変更するには、その Color プロパティを設定します。
p.Color = 'red';
親と子
MATLAB では、グラフィックス オブジェクトが階層的に構成されています。階層の最上位は、"グラフィックス ルート" と呼ばれる特別なオブジェクトです。グラフィックス ルートにアクセスするには、関数 groot を使用します。
groot
ans =
Graphics Root with properties:
CurrentFigure: [1×1 Figure]
ScreenPixelsPerInch: 100
ScreenSize: [1 1 1280 1024]
MonitorPositions: [1 1 1280 1024]
Units: 'pixels'
Show all properties
すべてのグラフィックス オブジェクト (ルートを除く) には親があります。たとえば、axes の親は figure です。
ax = gca; ax.Parent
ans =
Figure (1) with properties:
Number: 1
Name: ''
Color: [1 1 1]
Position: [167 243 560 337.1852]
Units: 'pixels'
Show all properties
多くのオブジェクトには子もあります。この axes には、2 つの text オブジェクトと 1 つの line オブジェクトという 3 つの子があります。
ax.Children
ans = 3×1 graphics array: Text (pi) Text (sin(x)) Line
axes には複数の子があるため、Children プロパティの値はグラフィックス オブジェクトの配列になります。axes の子に個別にアクセスするには、この配列にインデックスを付けます。これにより、子オブジェクトのプロパティを設定できるようになります。
t = ax.Children(2);
t.FontWeight = 'bold';
グラフィックス オブジェクト配列の事前割り当て
MATLAB では、配列を使用する前の事前割り当てをお勧めしています。グラフィックス オブジェクトの配列に事前の割り当てを行うには、gobjects コマンドを使用します。これにより、グラフィックス オブジェクトを配列に追加できるようになります。
objarray = gobjects(1,5); objarray(1) = f; objarray(2) = ax; objarray(3) = p; objarray(4) = txt1; objarray(5) = txt2; objarray
objarray =
1×5 graphics array:
Figure Axes Line Text Text
すべてのオブジェクト プロパティの取得
MATLAB のグラフィックス オブジェクトには多数のプロパティがあります。オブジェクトのすべてのプロパティを確認するには、get コマンドを使用します。
get(f)
Alphamap: [0 0.0159 0.0317 0.0476 0.0635 0.0794 0.0952 0.1111 0.1270 0.1429 0.1587 0.1746 0.1905 0.2063 0.2222 0.2381 0.2540 0.2698 0.2857 0.3016 0.3175 0.3333 0.3492 0.3651 0.3810 0.3968 0.4127 0.4286 0.4444 0.4603 … ] (1×64 double)
AutoResizeChildren: off
BeingDeleted: off
BusyAction: 'queue'
ButtonDownFcn: ''
Children: [1×1 Axes]
Clipping: on
CloseRequestFcn: 'closereq'
Color: [1 1 1]
Colormap: [256×3 double]
ContextMenu: [0×0 GraphicsPlaceholder]
CreateFcn: ''
CurrentAxes: [1×1 Axes]
CurrentCharacter: ''
CurrentObject: [0×0 GraphicsPlaceholder]
CurrentPoint: [0 0]
DeleteFcn: ''
DockControls: on
FileName: ''
GraphicsSmoothing: on
HandleVisibility: 'on'
Icon: ''
InnerPosition: [167 243 560 337.1852]
IntegerHandle: on
Interruptible: on
InvertHardcopy: on
KeyPressFcn: ''
KeyReleaseFcn: ''
MenuBar: 'none'
Name: ''
NextPlot: 'add'
Number: 1
NumberTitle: on
OuterPosition: [167 243 560 337.1852]
PaperOrientation: 'portrait'
PaperPosition: [1.4500 3.8141 5.6000 3.3719]
PaperPositionMode: 'auto'
PaperSize: [8.5000 11]
PaperType: 'usletter'
PaperUnits: 'inches'
Parent: [1×1 Root]
Pointer: 'arrow'
PointerShapeCData: [16×16 double]
PointerShapeHotSpot: [1 1]
Position: [167 243 560 337.1852]
Renderer: 'opengl'
RendererMode: 'auto'
Resize: on
Scrollable: off
SelectionType: 'normal'
SizeChangedFcn: ''
Tag: ''
Theme: [1×1 matlab.graphics.theme.GraphicsTheme]
ThemeChangedFcn: ''
ThemeMode: 'auto'
ToolBar: 'none'
Type: 'figure'
Units: 'pixels'
UserData: []
Visible: off
WindowButtonDownFcn: ''
WindowButtonMotionFcn: ''
WindowButtonUpFcn: ''
WindowKeyPressFcn: ''
WindowKeyReleaseFcn: ''
WindowScrollWheelFcn: ''
WindowState: 'normal'
WindowStyle: 'normal'
XDisplay: ':99'