1
votes

Is it possible to create a persistent variable in a MATLAB function with an arbitrary data type? For example, I would like to create a persistent containers.Map variable in my MATLAB function. How can I do that?

2
@Dev-iL I don't really want to enforce any data type, I just want my persistent variable in a function to be of a certain type. There is nothing to enforce because no one can "set" the variable inside the function, it just used for memorization purposes. - Ant
Perhaps we understand the word "arbitrary" differently. When you say you want an "arbitrary" data-type, this is the default MATLAB way, where a variable with a certain name can be assigned anything you want. If you want it to be of a specific class (e.g. double, char etc.) - the data-type is no longer arbitrary!, but rather very specific (or "enforced", as I put it). Of course your are free to do with your question what you want, but I think my edit helped clarify the intention better. - Dev-iL
@Dev-iL Of course after I instantiate it the data type is no longer arbitrary; the point is that a priori it is arbitrary. Also, when I use "persistent" matlab will instantiate the object to be a double. So my question is: How can I make it so that matlab instantiates these objects to a particular data type that I choose? Arbitrary a priori, specific a posteriori (after the instantiation) - Ant
@Dev-iL I don't feel that enforcing is the right term, because that suggests someone trying to change the data type and matlab rejecting the change; which is what your answer was about. Again, very cool functionality but not what I asked :) - Ant
Could you perhaps explain your use case? I fail to see the benefit in what you're trying to do. Can't you achieve the same effect by assigning whatever it is you want into the persistent variable when it's empty, thus specifying its type a posteriori? - Dev-iL

2 Answers

2
votes

All variables defined as persistent using the persistent keyword are initialized to an empty array of the double datatype. You can re-initialize them to whatever datatype you want by checking if they are empty using isempty and performing the initialization then. You can also check to ensure that it is a double just in case you don't want to force re-initialization if you have an empty containers.Map object.

function persist(key, value)
    persistent container

    if isa(container, 'double') && isempty(container)
        container = containers.Map();
    end

    container(key) = value;
end
2
votes

MATLAB class properties can be used for this, since they can be restricted to specific datatypes. For example:

classdef foo
    properties
        prop@char scalar = 'A'  % will only accept char inputs, not numeric 
    end
end

Then every method that tries to update this.prop would fail if the provided values is of an incorrect type. If the update succeeds, you can update the persistent variable too.

This functionality was officially incorporated into R2017a, but an undocumented way has been known for a while now.

For further reading see this blog post.