1

I'm learning how to program MATLAB GUI's and am using GUIDE. I'm curious what is considered better practice: using the MATLAB 'set( )' function to edit an objects property, or simply the dot operator to edit the objects property.

Say for example I had an editable text box. The tag of this text box is 'string_fs'. There are two ways that I've found to change the string of this object:

handles.string_fs.String = 'hello';

or

set(handles.string_fs, 'String', 'hello');

Is there any benefit of one method vs the other?

Suever
  • 64,497
  • 14
  • 82
  • 101
Izzo
  • 4,461
  • 13
  • 45
  • 82
  • 1
    For the sake of readability I prefer the dot notation for single property access and `set`/`get` for multiple properties. On the other hand if I need to consider backward compatibility, `set`/`get` is the way to go as dot notation was first slowly added in `2014b`. – Skogsv Aug 09 '16 at 06:00
  • There might be _very tiny_ performance difference, but in general I would follow @Skogsv advice: Just make your choice between readability and backward code compatibility. – Hoki Aug 09 '16 at 10:14

1 Answers1

2

This is mainly a personal preference unless you need to write code which will work in pre-HG2 versions of MATLAB (before R2014b) in which case you would need to use set and get to maintain backwards compatibility.

Here are a few other advantages to using set and get methods:

Manipulation of Multiple Objects Simultaneously

You can have an array of objects or handles and change a property on all of them simultaneously

ax(1) = subplot(1,2,1);
ax(2) = subplot(1,2,2);

% Change the font weight of both axes object to be the same
set(ax, 'FontWeight', 'bold')

% Change the font weight to be different for each
set(ax, {'FontWeight'}, {'bold'; 'normal'})

You can do something similar with dot notation but I think it's a little less readable

[ax.FontWeight] = deal('bold');

Changing Multiple Properties Simultaneously

You can, in one statement, change the values of multiple properties which I think can help with readability and keep your code concise.

set(ax, 'FontSize', 20, 'FontWeight', 'bold', 'FontName', 'arial')

As pointed out in the comments by @Hoki, this is particularly important if you are updating properties that are inter-dependent. For example modifying the XData and YData of a plot where they both need to be the same size.

So this:

hplot = plot(1, 1);
set(hplot, 'XData', rand(10, 1), 'YData', rand(10, 1))

Instead of this:

hplot = plot(1, 1);

set(hplot, 'XData', rand(10, 1))
% Plot won't render here

set(hplot, 'YData', rand(10, 1))
% Plot will be able to render

Programmatically Get Possible Values

With dot notation, you can use tab completion to get a list of possible values; however, you can do this programmatically with set by simply not providing a value.

possible = set(axes, 'FontWeight')

%   'normal'
%   'bold'  

Shortened and Case-Insensitive Properties

I don't recommend using these next two, but they are possible with set and get.

With the set and get methods you don't have to provide an entire properties name, just enough letters that it's unique.

set(ax, 'FontW', 'bold')

Also when using set and get, the property name is case insensitive

set(ax, 'fontweight', 'bold')
Suever
  • 64,497
  • 14
  • 82
  • 101
  • another example where you _have_ to use `set` is when you need to change **multiple properties in the same instruction** (otherwise Matlab can throw a fit). For example, this can happen often when changing `FaceColor` property of a graphic object from `'flat'` to `'interp'` (if you don't also assign simultaneously the `FaceVertexCData` property, Matlab throw a warning and stop rendering). A common occurrence is also if you want to change the `min`, `max` and/or current `value` of a slider (if the new `max` conflict with the old `min`), etc ... – Hoki Aug 09 '16 at 16:29
  • Ha ha ... I went for some complicated examples to illustrate it and you put the most common reason (which is the biggest reason I use `set` too). Well done! – Hoki Aug 09 '16 at 19:59