MATLAB Answers

Equivalent of c++'s NULL or python' s None in MATLAB

28 views (last 30 days)
Fawad Khan
Fawad Khan on 20 May 2021
Answered: Bruno Luong on 21 May 2021
I have a c++ code where certain variables (as well as certain user defined class objects) are set to NULL under some conditions. I'm translating this code to MATLAB. Should I assign those variables/class objects as [] (and use isempty to check conditions) or should I assign them NaN (and use isnan to check conditions)? What would be more correct and efficient? Or do i assign them to logical operator "false"?

Accepted Answer

Walter Roberson
Walter Roberson on 20 May 2021
C / C++ NULL takes up space, as does python None . If you need something that takes up space, then using [] would not be appropriate.
C / C++ NULL is a valid member of a pointer class. You can store it in a single array of pointer type -- and it often is stored that way. For example if you create a common two-level array, like int** for a pointer to an array of pointers to int, then it would be common to set the unused slots to NULL, and there are no class problems in doing that.
python None is a different class. If you have an object which is expected to be uniform data type, then it cannot have None stored in it.
MATLAB's NaN exists in single precision and double precision, and those are full members of those classes - the IEEE 754 standard defines specific bit patterns for NaN, just like it defines specific bit patterns for infinity. So NaN is suitable for use in a homogenous single or double array situation, and would still be able to test for special value within that class. Contrawise, NaN cannot be used in a homogenous uint8 array or any other class other than single or double.
logical values are not any numeric class, but they silently convert to numeric in many situations. So [1, 2, false, 4] would work, but it would convert to [1, 2, 0, 4] and you would then lose being able to test each entry for special value.
If your purpose is to signal "no value" in a context where it is expected that the results will be assigned to a variable, then using [] would be more MATLAB-y. If you need the value to take up space, then use NaN if regular values would be single or double.
In some cases it makes the most sense to return a logical vector of success / failure. An example of this is ismember(), where the first output is success/failure and the second output is the index location.

More Answers (3)

James Tursa
James Tursa on 20 May 2021
Edited: James Tursa on 20 May 2021
This will probably depend on how these are used downstream in your code and whether you have arrays of them to deal with. If they are scalar variables, then probably either [] or nan will work. If they are only being used for flags, then logical true/false would work and is probably the fastest. If there are arrays of them, then you are probably stuck with nan or logical. If you are processing arrays of them downstream in your code beyond just flag checking, many functions can automatically deal with and/or ignore the nan, so again nan might be your best choice.
Bottom line is I wouldn't worry so much about the efficiency of isempty( ) vs isnan( ) vs logical checking. I would pay more attention to how they will be used downstream in your code. Hard to advise any more than this without knowing more about what the code is doing.

Steven Lord
Steven Lord on 20 May 2021
In addition to what James and Walter have written, if you're writing your own class and want to be able to indicate that some element of an array of instances of your class are "missing" you could implement your class to satisfy the missing contract. If you do I recommend you write a test that it correctly satisfies the contract using the matlab.test.behavior.Missing class as the base class for that test.

Community Treasure Hunt

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

Start Hunting!