For uicontrol, why can't "UserData" property hold a structure but 'UserData' can?

1 次查看(过去 30 天)
While converting code in 2018b to use strings as much as possible, ran into this surprising error:
f = figure;
s.a = 1;
uicontrol(f, "String", "Hello", "UserData", s);
The error is "Incorrect number of input arguments". However, the following do not generate errors:
uicontrol(f, "String", "Hello", 'UserData', s);
or
a = 1;
uicontrol(f, "String", "Hello", "UserData", a);
It seems that UserData as a string when naming the property and giving it a structure causes the trouble.
Any thoughts out there?

采纳的回答

Rik
Rik 2018-9-26
One of the many places where using strings instead of char arrays will trip you up. I haven't searched for one, but I've never yet seen a situation where strings solved a problem in such a simple and elegant way that it warrants rewriting half of Matlab for.
I'd suggest looking if there is already a bug report (and create it if there isn't). They will either solve it in an update to R2018b, or in a later release. Until then, just use char arrays. I'm also still using get and set instead of dot-indexing, because I generally don't need to go multiple levels deep (which is the only situation where dot-indexing is better). Fun fact: in set and get the properties are not case sensitive, while with dot-indexing they are.
  1 个评论
Craig DeAlmeida
Craig DeAlmeida 2018-9-27
I created a bug report as suggested. I think newer MATLAB users (as those around me) will gravitate more towards strings, as the char array treatment of text is an unusual feature of MATLAB. That said, I'm totally used to it and so it was a tough call to change my code, but I think it will ultimately benefit newer users of MATLAB and my code.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by