필터 지우기
필터 지우기

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