What is the difference between using set/invoke and dot notation?
4 views (last 30 days)
Show older comments
I've had to work with an Excel automation server a few times and I'm confused about the difference between using set or invoke and using dot notation.
for example the following two lines appear to function identically:
Workbook = Excel.Workbooks.Open(fileName);
Workbook = invoke(Excel.Workbooks, 'open', fileName);
Same with these two lines as well:
Sheets.Item('TestSheet').Rows(1).font.bold = 1;
set(Sheets.Item('TestSheet').Rows(1).font,'bold', 1)
What is difference. Why would I use one method over the other?
0 Comments
Answers (1)
Daniel Shub
on 2 Oct 2012
The processing is slightly different with the two methods, but in general, they tend to produce the same result.
Workbook = invoke(Excel.Workbooks, 'open', fileName);
Determines the class of Excel.Workbooks and then calls the invoke method of that class and passes it Excel.Workbooks, 'open', and fileName.
Workbook = Excel.Workbooks.Open(fileName);
Determines the class of Excel.Workbooks and then calls the subsref method of that class and passes it all sort of information. The subsref method then usually will call invoke(Excel.Workbooks, 'open', fileName).
If the class is using the built-in subsref function, the two behave nearly identically. The overhead of the built-in subsref is small. If the class has an overloaded subsref, you can get all sorts of odd behavior. Further, in my experience, the overhead of an overloaded subsref method is quite large.
2 Comments
Image Analyst
on 2 Oct 2012
I'd been wondering that myself. So it kind of sounds like the direct way of calling is simpler and preferable to the invoke way. If there's any advantage to using invoke(), please let us know.
Daniel Shub
on 2 Oct 2012
I probably should have added in the answer that I am not sure if activeX objects behave like other MATLAB objects. For standard OOP I think the direct call is better (e.g., invoke). If there is an overloaded SUBSREF method the direct call will skip the call to |subsref which can/might save time. That said, if my class needs an overloaded SUBSREF, I tend to try and run away.
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!