class of returns from get() with 1 vs. mutliple handles

Get(h,'parameter') always returns a cell if h contains more than one handle, but if h is a single handle it returns a value of the class of the parameter.
The result is that a special case needs to be written to handle the return if h is of length 1. Is there a way to avoid this so that get always returns a cell even if it is of length one?
Example: >> for i=1:5;h(i)=line(i,i);set(h(i),'userdata',x);end
>> class(get(h,'userdata')) ans = cell
>> class(get(h(i),'userdata')) ans = double

 採用された回答

Matt Fig
Matt Fig 2011 年 3 月 7 日

2 投票

To force a cell array, use:
plot(1:10,1:10,1:12,1:12); % Example.
H = get(gca,'children') % Multiple handles.
C1 = get(H,{'linestyle'}) % A cell array, 2-by-1.
C2 = get(H(1),{'linestyle'}) % A cell array, 1-by-1.

2 件のコメント

Fangjun Jiang
Fangjun Jiang 2011 年 3 月 7 日
Thanks, Matt! Good to know your trick.
Brett Shoelson
Brett Shoelson 2011 年 3 月 8 日
Very nice, Matt!

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

その他の回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2011 年 3 月 7 日

1 投票

I don't have an answer but I want to second your opinion. I found this inconvenient many times. What I usually do is to add a line to process the result.
if ~iscell(answer),answer={answer};end
Peter
Peter 2011 年 3 月 8 日

0 投票

Thanks - that's neat solution.

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

タグ

質問済み:

2011 年 3 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by