Reputation: 103
I want to find a way to get the name of an instance of an object in MATLAB and return it within itself in MATLAB R2019b.
So in the Launcher Constructor define the instances like this
Properties
TorqueViewComponent
SpeedViewComponent
end
methods
function obj = Launcher()
TorqueViewComponent = GraphView(args...);
SpeedViewComponent = GraphView(args...);
end
end
and then within the graph view object constructor something like
function obj = GraphView(args...)
if strcmp(object name, 'TorqueViewComponent')
do some things;
elseif strcmp(object name, 'SpeedViewComponent')
do some other things;
end
end
In essence I have multiple instances of the GraphView object, which I want to plot different sets of data based on the name of the instance.
If you have any questions let me know and I can go over the problem in a little more detail
Upvotes: 1
Views: 214
Reputation: 22215
You really only have two options; either subclass Graphview to create specialised subclasses, or pass an extra input argument. There might be some hacky solution to allow you to somehow get the name from the caller workspace, but this is highly unlikely to improve readability and maintainability of your code.
E.g.
classdef TorqueGraphView < GraphView
methods
function obj = TorqueGraphView(varargin)
obj@SuperClass(varargin);
...
end
end
end
In fact, if you don't really need to pass extra arguments to TorqueGraphView, you can take advantage of the fact that you can make an Implicit Call to Inherited Constructor
classdef TorqueGraphView < GraphView
% ... no constructor, only Torque-specific methods here
end
Otherwise, if you think this is overkill for such small adjustments, you really have to pass an argument to indicate this. If your main concern is 'messiness', you can consider packaging your arguments into a struct, which looks a bit more tidy. This might even help you nicely separate object-specific arguments from generic / option related arguments, e.g.
TorqueArgs = struct( ...
'type' , 'torque' , ...
'initialvalue', 0 , ...
'ylabel' , 'Torque [Nm]' , ...
'plottitle' , 'Torque' )
GraphOpts = struct ( ...
'axescolor', 'k' , ...
'linewidth', 3, , ...
'grid' , 'on' )
TorqueViewComponent = GraphView( TorqueArgs, GraphOpts );
Upvotes: 2
Reputation: 30047
You added in the comments that this was to influence things like the ylabel
of the plot.
You could use inputname
to get around this, but you'd have to split the function out, as the variable needs to be defined first.
function ylabelVarName( v )
ylabel( inputname(1) )
end
Then
function obj = Launcher()
TorqueViewComponent = GraphView(args...);
ylabelVarName( TorqueViewComponent );
SpeedViewComponent = GraphView(args...);
ylabelVarName( SpeedViewComponent );
end
You could probably make this part of the GraphView
class, but I can't see how you'd get around doing it in two lines.
Really though, I think the correct answer is to just use an additional input argument for your GraphView
function which specifies your preferences in terms of axis labels etc.
Upvotes: 0