bindable

版本 2.0.2 (6.5 KB) 作者: Hyeokjin Jho
Superclass for data binding between object properties and MATLAB UI components — ideal for implementing MVVM patterns.
58.0 次下载
更新时间 2025/9/24

查看许可证

Bindable – Superclass for UI Data Binding in MATLAB
The bindable class provides a lightweight superclass that enables two-way data binding between model properties and MATLAB UI components (uifigure, uicontrol). By inheriting from bindable, you can connect object properties to UI controls with minimal setup, automatically synchronizing values between the model and the UI.
Features
  • Value binding: Synchronize scalar or array properties with UI controls (e.g., sliders, edit fields, checkboxes). Supports forward (model→UI), reverse (UI→model), or bidirectional binding.
  • Enumeration binding: Map enumeration-like structures (struct with Name and SelectedIdx) to dropdowns or listboxes, keeping selection states in sync.
  • Enable state binding: Tie logical properties to UI component enable/disable state.
  • Subscripted property binding: Directly bind array elements or struct fields using subscript notation (Prop(2), Prop.Field).
  • Automatic listener management: Internally stores and handles listeners for property changes and UI callbacks.
Dependencies
Examples
  • demo_bindable.m
Methods
bindValue
[FWB, RVB] = obj.bindValue(propname, targetObjects, options)
Bind a model property to one or more UI components. Keeps values synchronized between the object and UI controls.
  • Inputs
  • propname : Name of the property (supports subscripted syntax, e.g. 'Values(2)').
  • targetObjects : Handle(s) to UI controls (uicontrol, uifigure components).
  • options (name-value pairs):
  • direction : 'forward', 'reverse', 'bidirectional' (default).
  • forwardConverter : Function handle to transform value (model → UI).
  • reverseConverter : Function handle to transform value (UI → model).
  • Outputs
  • FWB : Binding handles for forward direction (model → UI).
  • RVB : Binding handles for reverse direction (UI → model).
Example:
bindableObj.MyValue = 3;
% Bidirectional binding of numeric property 'MyValue' with slider UI
bindableObj.bindValue('MyValue',sliderHandle);
% Can bind multiple UI objects
[fw, rv] = bindableObj.bindValue('MyValue',[sliderHandle, editboxHandle], ...
forwardConverter=@round, ...
reverseConverter=@double);
bindEnum
[FWB, RVB] = obj.bindEnum(propname, targetObjects, options)
Bind an enumeration-like property (struct with fields Name, SelectedIdx) to list-type controls (uidropdown, uilistbox).
  • Inputs
  • propname : Name of enumeration property.
  • targetObjects : Dropdown or listbox handles.
  • options.direction : 'forward', 'reverse', or 'bidirectional' (default).
  • Outputs
  • FWB : Forward binding handles (model → UI).
  • RVB : Reverse binding handles (UI → model).
Example:
bindableObj.Choices = struct('Name',{{'Red','Green','Blue'}},'SelectedIdx',2);
% Synchronize enum struct to dropdown
bindableObj.bindEnum('Choices',dropdownHandle);
bindEnabledState
H = obj.bindEnabledState(propname, targetObjects, options)
Bind a logical property to the 'Enable' state of one or more UI controls.
  • Inputs
  • propname : Logical property name.
  • targetObjects : UI component handles.
  • options.forwardConverter : Function handle mapping property value to logical (default = identity).
  • Output
  • H : Binding handle for forward direction (model → UI).
Example:
bindableObj.MyLogical = false;
% Enable or disable a button based on model.MyLogical
bindableObj.bindEnabledState('MyLogical',buttonHandle);
bindableObj.MyNumber = 3;
% Enabled state (logical) can be derived from non logical value using forwardConverter
bindableObj.bindEnabledState('MyNumber',buttonHandle, ...
forwardConverter=@(x) x > 2);
Binding callbacks can be programmatically triggered using binding.execute
[fwdBinding, revBinding] = bindableObj.bindValue('MyValue',sliderHandle);
fwdBinding.execute % this updates UI (sliderHandle)
revBinding.execute % this updates bindableObj

引用格式

Hyeokjin Jho (2025). bindable (https://ww2.mathworks.cn/matlabcentral/fileexchange/90885-bindable), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2021a
与 R2020a 及更高版本兼容
平台兼容性
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
2.0.2

edit description

2.0.1

Using old name-value pair argument notation in demo file for compatibilty
More detailed description

2.0.0

Changed to superclass

1.0.1

Updated description about modelObject

1.0.0