MATLAB Answers

Setting different properties for some line objects stored in the same handle

2 ビュー (過去 30 日間)
KAE
KAE 2020 年 1 月 6 日
コメント済み: KAE 2020 年 1 月 6 日
Let's say I have a handle h1 that contains several line objects, like so:
h1 = plot(1:10, rand(10, 25)); % h1 contains 10 line objects
I want to change the properties of some but not all of the lines, such as making the 3rd and 5th lines wider. How do I do this? Here is what I have tried,
h1([3 5]).LineWidth = 2; % Try to change two of the lines at once
% Expected one output from a curly brace or dot indexing expression, but there were 2 results.
h1({[3 5]}).LineWidth = 2; % Try to change two of the lines at once
% Unable to use a value of type cell as an index.
h1{3}.LineWidth = 2; % Try to change just one line
% Unable to perform assignment because brace indexing is not supported for variables of this type.
What do I need to do differently?

  0 件のコメント

サインイン to comment.

採用された回答

Fangjun Jiang
Fangjun Jiang 2020 年 1 月 6 日
set(h1([3,5]),'LineWidth',2)

  0 件のコメント

サインイン to comment.

その他の回答 (1 件)

Tom Holz
Tom Holz 2020 年 1 月 6 日
編集済み: Tom Holz 2020 年 1 月 6 日
You can do this with the old-style set() functions, like this:
set(h1([3 5]), 'LineWidth', 2);
Or you can use deal(), like this:
[ h1([3 5]).LineWidth ] = deal(2);
I don't believe there are functional difference between the two.

  3 件のコメント

KAE
KAE 2020 年 1 月 6 日
Very cool use of deal.
Tom Holz
Tom Holz 2020 年 1 月 6 日
Thanks. I tend to avoid deal() in practice because I think the code is slightly more confusing to look at. And I also like how the property name is colored differently when using set() because it's quoted text. That makes it easy to scan code and see property names in a different color.
KAE
KAE 2020 年 1 月 6 日
Yes, I think I will remember the 'set' syntax better than deal myself. Still, cool to see.

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by