"Unrecognized method, property, or field 'Type' for class 'matlab.ui​.control.W​ebComponen​t'."

20 次查看(过去 30 天)
In App Designer (MATLAB R2024a, Windows 10 Pro, 64-bit) I have a tab (app.tabFiles) that has a few ui components on it. When I query the Type property on all of them at once, I get the following error:
tf = strcmp({app.tabFiles.Children.Type}, 'uistatebutton') ; % This works fine with 7 other tabs, panels, etc
"Unrecognized method, property, or field 'Type' for class 'matlab.ui.control.WebComponent'."
However, if I query the Type of the components individually, each happily returns its Type. Huh?
I should mention, there are 13 components visible on the tab and 13 listed in the Component Browser. However, the tab has 14 Children - the extra child is the label (set to empty string) on a uieditfield component - but all respond as expected to the individual Type request.
Any idea what would cause that exception to be thrown? And how to avoid it?

采纳的回答

Fangjun Jiang
Fangjun Jiang 2024-4-14
编辑:Fangjun Jiang 2024-4-14
I think app.tabFiles.Children is an array of handles. Try tf = strcmp(get(app.tabFiles.Children,'Type'), 'uistatebutton')
  2 个评论
Dave Watson
Dave Watson 2024-4-16
Yes, thank you, that works. However, it should be equivalent to what I did and my approach works on other UI components with Children, with that one exception. Mysteries of MATLAB! Thanks, again.
Fangjun Jiang
Fangjun Jiang 2024-4-16
编辑:Fangjun Jiang 2025-9-11,14:13
Here is another example that the "dot property name" method does not work on arry of objects.
close all;plot(1:3);
obj=findobj(0)
obj =
4x1 graphics array: Root Figure (1) Axes Line
obj(1).Type
ans = 'root'
get(obj,'Type')
ans = 4x1 cell array
{'root' } {'figure'} {'axes' } {'line' }
obj.Type
Unrecognized method, property, or field 'Type' for class 'matlab.graphics.Graphics'.

请先登录,再进行评论。

更多回答(1 个)

Viren
Viren 2025-9-11,11:28
编辑:Viren 2025-9-11,11:30
Thanks @Fangjun Jiang! Your solution to use v = get(h,propertyNames) and set(h,NameArray,ValueArray) functions worked perfectly for me. I had a slightly different setup:
I was working with a GUI tab that contains a grid layout named GridLayout_LabelCheckboxAndSpinner, which holds 34 UI components - a mix of checkboxes, labels, and spinners. When I ran:
app.GridLayout_LabelCheckboxAndSpinner.Children
I got a 34x1 graphics array, confirming that all components were correctly placed under the grid layout and each has an 'Enable' property.
However, when I tried to retrieve the 'Enable' property value of single component using:
singleComponentEnableProp = app.GridLayout_LabelCheckboxAndSpinner.Children(1).Enable
ans =
OnOffSwitchState enumeration
off
This technique worked for single component one after another, or you can either make a for loop to retrive all component's 'Enable'property.
However, when I tried to retrieve the 'Enable' property for all components at once using:
allComponentsEnablePropArray = app.GridLayout_LabelCheckboxAndSpinner.Children(:).Enable
I encountered the following error:
Unrecognized method, property, or field 'Enable' for class 'matlab.ui.control.internal.model.ComponentModel'.
This happened because I was trying to access all Children the 'Enable' property directly from the array, which doesn't work for heterogeneous UI components. These components are all part of the same graphics array, but they belong to different classes (e.g., matlab.ui.control.Label, matlab.ui.control.CheckBox, etc.).
Instead, I used the get function:
allComponentsEnablePropArray = get(app.GridLayout_LabelCheckboxAndSpinner.Children, 'Enable')
This successfully returned a cell array of 'Enable' states.
To restore or apply these states back to the components, I used:
arrayfun(@(i) set(app.GridLayout_LabelCheckboxAndSpinner.Children(i), 'Enable', allComponentsEnablePropArray{i}), 1:numel(allComponentsEnablePropArray))
This worked perfectly, assigning each 'Enable' value to its corresponding component.

类别

Help CenterFile Exchange 中查找有关 Create Custom UI Components 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by