MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

Robin L.
0

"set" VS "=" assignment

Robin L.
さんによって質問されました 2019 年 3 月 5 日
最新アクティビティ Robin L.
さんによって コメントされました 2019 年 3 月 8 日
Hello !
I am writing about the two assignment methods : "set function" and "= operator". For example let's take a small code that maximize my App Designer window :
app.UIFigure.WindowState = 'maximized';
% OR
set(app.UIFigure, 'WindowState', 'maximized');
I am wondering in terms of performance/speed, what is best way to proceed ?
Thank you !

  3 件のコメント

Robin L.
2019 年 3 月 5 日
I was thinking that the set function may have been developed to enable the use of dynamic variables in property name ? Like :
p = 'WindowState';
set(app.UIFigure, p, 'maximized');
Rik
2019 年 3 月 5 日
As a further remark: you can use something similar with object notation.
clc
f=figure(1);
try
prop='Name';
f.(prop)='xyz';
catch ME
disp(ME.message)
end
Another upside to object notation is that you can make a longer sequence:
parentobj.childobj.someprop='value';
%versus
child=get(parentobj,'childobj');
set(child,'someprop','value')
%or shorter, but less readable:
set(get(parentobj,'childobj'),'someprop','value')
Robin L.
2019 年 3 月 8 日
Thank you so much Rik Wisselink !

サインイン to comment.

1 件の回答

Rik
回答者: Rik
2019 年 3 月 5 日
 採用された回答

The set syntax is older than the object notation.
The advantages of the set syntax are that you can use it in older Matlab releases, you have more flexibility in how to assign property names, and that you can use incomplete names and incorrect capitalization.
To show the flexibility, try running this:
clc
f=figure(1);
set(f,'nam','abc')%stupid, but it still works
try
f.nam='foo';
catch ME
disp(ME.message)
end
try
f.name='bar';
catch ME
disp(ME.message)
end
However, this does come at the price of some performance: set seems to be about twice as slow, depending on the task. Below you find a tester that shows the performance for two simple tasks. The timings are a bit misleading, because graphics updates will be machine/OS depedent and will introduce more lag. This extra lag should be the same for either syntax.
%open 100 figures for testing
clc,close all
handles=cell(1,100);
for n=1:size(handles,2)
handles{n}=figure('Name','abc','Position',[0.2 0.2 0.2 0.2]);
end
%measure the time it takes to modify the title and the position properties
%first for the set syntax
tic
for n=1:n
set(handles{n},'Name','foo')
set(handles{n},'Position',[0.3 0.3 0.2 0.2])
end
t_set=toc;
%now with object notation
tic
for n=1:n
handles{n}.Name='bar';
handles{n}.Position=[0.1 0.1 0.2 0.2];
end
t_obj=toc;
close all
fprintf(['set syntax takes about %.2f ms per iteration,\n',...
'object notation takes about %.2f ms per iteration\n'],...
t_set*1000/n,t_obj*1000/n)
This returns this on my machine:
set syntax takes about 0.19 ms per iteration,
object notation takes about 0.10 ms per iteration

  1 件のコメント

Robin L.
2019 年 3 月 8 日
Thank you so much Rik Wisselink !
I didn't find something in the official documentation. Thank to you ;)

サインイン to comment.



Translated by