Missing in Matlab: method chaining or method cascading ?

8 views (last 30 days)
Does Matlab allow chaining or cascading of methods defined in a class?
I am trying to avoid long nested lists of switch/case . With a chain of methods I would like to take the sqrt of a matrix and eventually the log10 (or not, depending on the current choice). Subsequently I'd like to use imagesc to display the result.
I could imagine something like this:
diffractionPatternObject.sqrt.log10.show(intensity)
At the moment I am stuck with the following Matlab code which requires to pass the arguments unit (decides if sqrt will be taken) and scale (decides if log10 will be taken). In my method show an ugly cascade of switch/case is required to distinguish the different cases. Most important I have to use two time the same switch/case because I don't know how to select the data interactively in the method. For this reason it would also be nice if I were able to select the data on which the method chain is executed on.
classdef diffractionPattern < handle
properties
intensity
end % end of properties
properties (Dependent)
amplitude
end % of (Dependent) properties
methods
% contructor
function dP = diffractionPattern(singleDiffractionPattern)
if nargin == 1 && isnumeric(singleDiffractionPattern)
dP.intensity = singleDiffractionPattern;
else
error(['No valid diffraction pattern was provided.' ...
'Please use a numeric matrix as argument.']);
end
end % of constructor
function show(dP, unit, scale)
figure
switch unit
case 'intensity'
switch scale
case 'lin'
imagesc(dP.intensity)
ylabel(colorbar, 'intensity counts')
case 'log'
imagesc(log10(dP.intensity))
ylabel(colorbar, 'log10 of intensity counts')
end
case 'amplitude'
switch scale
case 'lin'
imagesc(dP.amplitude)
ylabel(colorbar, 'amplitude')
case 'log'
imagesc(log10(dP.amplitude))
ylabel(colorbar, 'log10 of amplitude')
end
end
axis image;
axis off;
end
function amplitude = get.amplitude(dP)
amplitude = sqrt(dP.intensity);
disp('intensities were converted to amplitudes')
end
end % of method
end % of classdef
I have searched the internet for some time without getting any answer on my search terms ( method chaining or method cascading in matlab)?
It seems that chaining is a feature of python and java for example. For more information on chaining or cascading see https://en.wikipedia.org/wiki/Method_chaining .
I am running Matlab 2013b on Windows 7.

Answers (1)

Steven Lord
Steven Lord on 8 Jul 2015
Instead of trying to invoke the methods using "dot syntax"
diffractionPatternObject.sqrt.log10.show('intensity')
try using "function syntax"
show(log10(sqrt(diffractionPatternObject)), 'intensity')
As long as the SQRT and LOG10 methods of the diffractionPattern class return a diffractionPattern object themselves, this should work. In a few specific situations described in the documentation there can be a difference between those two syntaxes, but with the example above as long as you defined your SQRT and LOG10 methods correctly you should be fine.
  1 Comment
Max Rose
Max Rose on 9 Jul 2015
Hi Steve, thank you very much for this "easy" workaround to reach my actual aim. Probably there is no need for me to redefine SQRT and LOG10, so your suggestion will work. But still I am not sure whether the concept of chaining is present in Matlab at all? I am planning a complex class definition and I try to figure out how detailed it can be set up and how the usability will be in the end. Chaining (using "dot syntax") is quite appealing I would say. A mixture between dot and function syntax may be confusing to the end-user of the class.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!