Missing in Matlab: method chaining or method cascading ?
8 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
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.
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!