フィルターのクリア

Save an arbitrary number of figures from outside a function

1 回表示 (過去 30 日間)
Scott Harris
Scott Harris 2020 年 11 月 13 日
コメント済み: Walter Roberson 2020 年 11 月 14 日
I have a function that returns a figure:
function fig = makefigure()
fig = figure()
end
This allows me to call the function from elsewhere and programatically save the figure that it makes. I'd like to be able to do a similar thing, but for an arbitrary number of figures. For example maybe something like:
function figs = makefigures(n)
for i = 1:n
%make a new figure here for any arbitrary number of figures, n
end
end
The problem is, I can't figure out how to return multiple figure handles from one function without specificying an explicit number of outputs (which I don't want to do). I tried putting the handles into a structure but this didn't work either.
Any thoughts on how to do this? Object oriented solutions are welcome (if that changes anything) because all this code is wrapped up inside of a classdef.

採用された回答

Walter Roberson
Walter Roberson 2020 年 11 月 13 日
function figs = makefigures(n)
figs = gobjects(1,n);
for i = 1:n
figs(n) = figure();
end
end
  2 件のコメント
Scott Harris
Scott Harris 2020 年 11 月 14 日
Thanks, was unware of gobjects!
quick note too, the line inside the forloop should use i to index, not n:
figs(i) = figure();
Walter Roberson
Walter Roberson 2020 年 11 月 14 日
gobjects() is not strictly necessary there, but it is a good idea. It is pre-allocating graphics handles, similar to initializing an array to its proper size by using zeros()

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by