Return figure handle from a function

27 ビュー (過去 30 日間)
Srikanth Nayak
Srikanth Nayak 2017 年 7 月 12 日
コメント済み: Srikanth Nayak 2017 年 7 月 12 日
I have a function that plots two variables and returns the plot handle. This function is currently returning a line object instead of a figure. How can I force MATLAB to return a figure?
%%Plotting function
function li = liner(a,b)
li = plot(a,b);
end
%% From prompt >> a =0:1:10; >> c = liner(a, 2*a); >> class(c)
ans =
'matlab.graphics.chart.primitive.Line'

採用された回答

Jan
Jan 2017 年 7 月 12 日
編集済み: Jan 2017 年 7 月 12 日
function FigH = liner(a,b)
LineH = plot(a,b);
FigH = ancestor(LineH, 'figure');
end
This replies the figure the plot is created it. This works if the figure was opened before, or implicitely by the plot command. If you want a new figure in every case:
function FigH = liner(a,b)
FigH = figure;
plot(a,b);
end
  1 件のコメント
Srikanth Nayak
Srikanth Nayak 2017 年 7 月 12 日
This works. Thanks a lot!

サインインしてコメントする。

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2017 年 7 月 12 日
Try this?
%%Plotting function
function li = liner(a,b)
li = figure;
plot(a,b);
end

カテゴリ

Help Center および File ExchangeGraphics Objects についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by