12

In C, I might do something like this:

#define MAGIC_NUMBER (88)

int foo(int a, int b, int c) {
  return a + b + c + MAGIC_NUMBER;
}

double bar(double x, double n) {
  return x + n + MAGIC_NUMBER;
}

/*
 * ...and so on with many kind-of-long functions using
 * MAGIC_NUMBER instead of writing a literal 88 like so:
 */

double bar(double x, double n) {
  return x + n + 88;
}

What should I do in Matlab? (Needs to work across multiple files.)

Benjamin Oakes
  • 12,262
  • 12
  • 65
  • 83
  • 2
    related questions: http://stackoverflow.com/questions/1773850/constants-in-matlab and http://stackoverflow.com/questions/1389042/how-do-i-create-enumerated-types-in-matlab – Amro Feb 25 '10 at 22:27
  • I'm sorry, my answer was rubbish. I deleted it. – Peter Feb 25 '10 at 23:09
  • Funny you should say that, Peter, I ended up doing that with globals. – Benjamin Oakes Feb 26 '10 at 01:15
  • I actually wrote the first question you reference, Amro. I'm still toying with the `mpp` idea. This question is a bit different as the accepted answer in the other only works on a per-function basis and is more concerned with making constants immutable. – Benjamin Oakes Feb 26 '10 at 01:17

4 Answers4

6

You can define a global variable or declare a function which simply returns a constant value (the second possibility looks better).

gnovice
  • 125,304
  • 15
  • 256
  • 359
AVB
  • 3,994
  • 22
  • 21
1

There is no really good answer at the current time. If you want to just define a simple variable that is seen in your workspace, then

f00 = 88;

obviously works. But this will not be seen inside other functions. So you could define it as a global variable, but then you need to declare each variable as global inside every function that will need it. To me this seems a kludge. Slightly better IMHO, is to define a function m-file for foo.

function returnvalue = f00
% returns the scalar value of 88
returnvalue = 88;

(Note that I have a comment line here. This is returned when you call help foo, as well, lookfor will see that line too. Better help than this is recommended, but I am feeling lazy right now.)

As long as foo.m is on your search path, then this always returns the value 88. Note that I have included no arguments. But you could be more creative, and perhaps allow a size argument, so that foo(N) would behave as do zeros, ones and eye. This is the nice thing about using a function here. It can do exactly as you wish. So perhaps...

function returnvalue = f00(varargin)
% returns the scalar value of 88, or replicated elements if a size is supplied
% usage: foo
% usage: foo(N)
% usage: foo(N1,N2,...)
%
% arguments: 
%  N - a scalar or vector that denotes the number
%      of repeated elements (all 88) to be generated
%
%  returnvalue - a scalar (or vector/array) of
%      size indicated by N.
%
% see also: ones, zeros, eye

if (nargin == 0)
  % no arguments, so return a scalar 88
  returnvalue = 88;
elseif (nargin == 1) && isscalar(varargin{1})
  % be consistent with ones, zeros, eye, etc.
  returnvalue = repmat(88,[varargin{1},varargin{1}]);
elseif (nargin == 1)
  % the size is a vector already
  if isvector(varargin{1})
    returnvalue = repmat(88,varargin{1});
  else
    error('FOO:impropersize','Size argument cannot be a general array')
  end
elseif 
  % nargin must have been more than 1
  returnvalue = repmat(88,cell2mat(varargin));
end

I could probably do a bit better error checking above, but you should get the general idea.

  • Yes, it is. In fact, it does seem a bit overkill. The simple option I suggested first is not complex at all though. You can do something as carefully and completely as you wish. –  Feb 26 '10 at 17:37
1

I second AB's response, declare a function that simply returns a constant value.

The other possibility is to just #define whatever you want and preprocess your .m files using cpp. Then however you lose the interactive nature of Matlab developoment.

edgar.holleis
  • 4,803
  • 2
  • 23
  • 27
  • 2
    Using `cpp` was one of my first inclinations as well. But, per the `man` page: "The C preprocessor is intended to be used only with C, C++, and Objective-C source code. In the past, it has been abused as a general text processor. It will choke on input which does not obey C's lexical rules." – Benjamin Oakes Feb 26 '10 at 15:18
1

A local function like AVB says is nice, and can be make a bit more durable using persistent variables instead of global. Persistent variables can't be edited by or seen anything outside of the m file, so it's a bit more like using #define at the top of a .c file.

function ver = version( ver )
% #define version = 'something'
% returns '' if not set since last time m file was edited, els persistently
% returns the last value set

persistent ver_;

if nargin
    ver_ = char(ver);  % garentee the data type
else
    ver_ = char(ver_);  % garentee the data type
end

ver = ver_;

end

The first line in the parent function will set the version

version('6.00'); % version number of the current converter

And anywhere within the same .m file I want to use this #define equivalent, I can refer to it just like a variable.

myMat.convertVersion = version

Bret Dahme
  • 21
  • 2