4242 % Alignment of the icon
4343 IconAlignment (1 ,1 ) wt.enum.AlignmentState = wt.enum.AlignmentState.top
4444
45- % Default size of new buttons ('1x' or 'fit')
46- DefaultSize {mustBeMember( DefaultSize ,{ ' 1x ' , ' fit ' } )} = ' 1x '
45+ % Default size of new buttons ('1x', 'fit' or a number )
46+ DefaultSize ( 1 , 1 ) string {mustBeValidGridSize( DefaultSize )} = " 1x "
4747
4848 end % properties
4949
@@ -160,13 +160,21 @@ function update(obj)
160160 end % for idx = 1:numNew
161161
162162 % Update layout
163+
164+ % What is the default size?
165+ defaultSize = obj .DefaultSize ;
166+ if ~isnan(str2double(defaultSize ))
167+ defaultSize = str2double(defaultSize );
168+ end
169+
170+ % Set button grids
163171 if obj .Orientation == " vertical"
164- obj.Grid.ColumnWidth = {obj . DefaultSize };
165- obj.Grid.RowHeight = repmat({ obj .DefaultSize }, 1 , numNew );
172+ obj .Grid .RowHeight( numOld + 1 : numNew ) = {defaultSize };
173+ obj.Grid.ColumnWidth = obj .Grid .ColumnWidth( 1 );
166174 else
167- obj.Grid.ColumnWidth = repmat({ obj . DefaultSize }, 1 , numNew ) ;
168- obj.Grid.RowHeight = { obj .DefaultSize } ;
169- end % if obj.Orientation == "vertical"
175+ obj .Grid .ColumnWidth( numOld + 1 : numNew ) = { defaultSize } ;
176+ obj.Grid.RowHeight = obj .Grid .RowHeight( 1 ) ;
177+ end
170178
171179 end % function
172180
@@ -180,6 +188,26 @@ function onButtonPushed(obj,evt)
180188
181189 end % function
182190
191+ function updateGridForButton(obj , prop , value )
192+ % Update main grid properties to value
193+
194+ % Convert any text array or numeric array into a cell array
195+ value = convertCharsToStrings(value );
196+ if ~iscell(value )
197+ value = num2cell(value );
198+ end
199+
200+ % If cell is scalar, repeat value for every button
201+ if isscalar(value )
202+ value = repmat(value , 1 , numel(obj .Grid.(prop )));
203+ end
204+
205+ % Update button size
206+ nElements = min(numel(value ), numel(obj .Grid.(prop )));
207+ obj .Grid.(prop )(1 : nElements ) = value(1 : nElements );
208+
209+ end % function
210+
183211 end % methods
184212
185213
@@ -191,17 +219,48 @@ function onButtonPushed(obj,evt)
191219 value = obj .Grid .ColumnWidth ;
192220 end
193221 function set .ButtonWidth(obj ,value )
194- obj.Grid. ColumnWidth = value ;
222+ obj .updateGridForButton( " ColumnWidth" , value ) ;
195223 end
196224
197225 function value = get .ButtonHeight(obj )
198226 value = obj .Grid .RowHeight ;
199227 end
200228 function set .ButtonHeight(obj ,value )
201- obj.Grid. RowHeight = value ;
229+ obj .updateGridForButton( " RowHeight" , value ) ;
202230 end
203231
204232 end % methods
205233
206234
207- end % classdef
235+ end % classdef
236+
237+
238+ function mustBeValidGridSize(val )
239+ % Validate value is valid size for grid layout
240+
241+ % Value must be either 'fit' or '1x', or convertable to a number.
242+ numVal = str2double(val );
243+
244+ % Is value convertable to a number?
245+ if ~isnan(numVal )
246+ return
247+ end
248+
249+ % Is value "fit"?
250+ if strcmpi(val , " fit" )
251+ return
252+ end
253+
254+ % Is value a 1x, 2x, etc?
255+ valStripped = strip(val , " x" );
256+ numStripped = str2double(valStripped );
257+ if ~isnan(numStripped )
258+ return
259+ end
260+
261+ % Value was not valid. Throw a validation error
262+ ME = MException(' ButtonGrid:InvalidSize' , ...
263+ ' Value must be a text scalar specifying the keyword '' fit'' , numbers, or numbers paired with '' x'' characters.' );
264+ throwAsCaller(ME );
265+
266+ end
0 commit comments