4 に答える
FINDOBJ関数を使用して、現在の Figure のすべての線オブジェクトのハンドルを取得できます。
hline = findobj(gcf, 'type', 'line');
次に、すべての線オブジェクトのプロパティを変更できます。
set(hline,'LineWidth',3)
またはそれらの一部のみ:
set(hline(1),'LineWidth',3)
set(hline(2:3),'LineStyle',':')
idx = [4 5];
set(hline(idx),'Marker','*')
Figure 内のオブジェクトを操作するには、それらのハンドルにアクセスできる必要があります。プロット関数を使用して Figure を作成すると、これらはハンドルを返します。あなたの場合のように、Figure を開くときは、グラフィック オブジェクト ツリーをたどって、操作する特定の要素へのハンドルを見つける必要があります。
このページには、グラフィックス オブジェクトの構造に関する情報があります。
必要なハンドルへのパスは Figure によって異なりますが、例として、Figure が単純なplot
コマンドを使用して作成された場合、これは線のプロパティを変更する 1 つの方法になります。
x = 0:0.1:2;
plot(x,sin(x));
fig = gcf % get a handle to the current figure
% get handles to the children of that figure: the axes in this case
ax = get(fig,'children')
% get handles to the elements in the axes: a single line plot here
h = get(ax,'children')
% manipulate desired properties of the line, e.g. line width
set(h,'LineWidth',3)
@yuk の回答に加えて、伝説も描かれている場合は、
hline = findobj(gcf, 'type', 'line');
N x 3
行 (より正確には - )を返しlines plotted + 2x lines in legend
ます。ここでは、プロットされたすべての線が凡例にもある場合のみを見ていきます。
シーケンスは奇妙です: 5 行 (注意してください1 to 5
) がプロットされ、凡例が追加された場合、次のようになります。
hline:
1 : 5 th line (mistical)
2 : 5 th line (in legend)
3 : 4 th line (mistical)
4 : 4 th line (in legend)
5 : 3 th line (mistical)
6 : 3 th line (in legend)
7 : 2 th line (mistical)
8 : 2 th line (in legend)
9 : 1 th line (mistical)
10: 1 th line (in legend)
11: 5 th line (in plot)
12: 4 th line (in plot)
13: 3 th line (in plot)
14: 2 th line (in plot)
15: 1 th line (in plot)
解決策として(金曜日の夕方の先延ばし)、私はこの小さな赤ちゃんを作りました:
解決策 1:凡例をリセットしたくない場合
凡例があるかどうか、およびプロットされている行数を検出します。
hline = findobj(gcf, 'type', 'line');
isThereLegend=(~isempty(findobj(gcf,'Type','axes','Tag','legend')))
if(isThereLegend)
nLines=length(hline)/3
else
nLines=length(hline)
end
行ごとに適切なハンドルを見つけて、その行の処理を行います (対応する凡例行にも適用されます)。
for iterLine=1:nLines
mInd=nLines-iterLine+1
if(isThereLegend)
set(hline([(mInd*2-1) (mInd*2) (2*nLines+mInd)]),'LineWidth',iterLine)
else
set(hline(mInd),'LineWidth',iterLine)
end
end
これにより、すべてi-th
の行が になりwidth=i
、ここで自動化されたプロパティの変更を追加できます。
解決策 2:シンプルに保つ
凡例を取り除き、線を処理し、凡例をリセットします。
legend off
hline = findobj(gcf, 'type', 'line');
nLines=length(hline)
for iterLine=1:nLines
mInd=nLines-iterLine+1
set(hline(mInd),'LineWidth',iterLine)
end
legend show
これは、凡例を特定の場所に配置する必要がある場合などには適していない可能性があります。
ビューアーで行を右クリックして、そこでプロパティを変更することもできます。これにより、対応する「凡例」エントリも変更されます (少なくとも 2014b では変更されます)。