Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab equivalent to calling inside static class

Confer the following code:

classdef highLowGame
    methods(Static)
        function [wonAmount, noGuesses] = run(gambledAmount)
            noGuesses = 'something';
            wonAmount = highLowGame.getPayout(gambledAmount, noGuesses); % <---
        end
        function wonAmount = getPayout(gambledAmount, noGuesses)
            wonAmount = 'something';
        end
    end
end

Is there a way to call a static method of the same class (inside a static) method without having to write the class name? Something like "self.getPayout(...)" - in case the class turns out to get to 500 lines and I want to rename it.

like image 832
casparjespersen Avatar asked Nov 21 '12 22:11

casparjespersen


People also ask

How do you call a static method in MATLAB?

Calling Static Methods Invoke static methods using the name of the class followed by dot ( . ), then the name of the method: classname. staticMethodName(args,...)

How do you call a method in static class?

Example Explained Then, we call the static method by using the class name, double colon (::), and the method name (without creating an instance of the class first).

Can I call a static method inside a regular one?

If you have no object but just call a static method and in that method you want to call another static method in the same class, you have to use self:: . So to avoid potential errors (and strict warnings) it is better to use self .

What is Classdef MATLAB?

classdef Syntax Class definitions are blocks of code that are delineated by the classdef keyword at the beginning and the end keyword at the end. Files can contain only one class definition. The following diagram shows the syntax of a classdef block.


2 Answers

Not an answer to your question directly, but it's worth noting that you can also put "local functions" after the end of your classdef block in your class.m file, and these behave like private static methods, but you do not need to invoke them using the class name. I.e.

% myclass.m
classdef myclass
  methods ( Static )
    function x = foo()
      x = iMyFoo();
    end
  end
end
function x = iMyFoo()
  x = rand();
end
% end of myclass.m
like image 91
Edric Avatar answered Sep 28 '22 12:09

Edric


As far as I can tell, "no" with a "but". In general, you can only specify the static method with the class name. However, you can fake your way around the restriction since MATLAB has feval:

classdef testStatic

    methods (Static)
        function p = getPi()  %this is a static method
            p = 3.14;
        end
    end

    methods
        function self = testStatic()

            testStatic.getPi  %these are all equivalent
            feval(sprintf('%s.getPi',class(self)))
            feval(sprintf('%s.getPi',mfilename('class')))
        end
    end
end

Here, class(self) and mfilename both evaluate to 'testStatic', so the functions above end up evaluating 'testStatic.getPi'.

Or, alteratively, you can write a non-static method, self.callStatic; then always use that. Inside that, just call testStatic.getPi. Then you'll only need to change that one line.

like image 34
Pete Avatar answered Sep 28 '22 11:09

Pete