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
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.
I don't think you want to use a persistent variable here. But you do want to output your result. My guess is you want this function:
function x = annualbalance(x)
if x < 5000
x = x * 1.05
elseif x >= 5000
x = x * 1.10
end
You can call this function repeatedly like so:
moneys = 1000; % your start value
moneys = annualbalance(moneys);
moneys = annualbalance(moneys);
moneys = annualbalance(moneys);
moneys = annualbalance(moneys);
moneys = annualbalance(moneys);
moneys = annualbalance(moneys);
moneys % display the amount you have now after 6 years
If you want to see how many years you need to wait to get a certain amount, you can use a loop as follows:
moneys = 1000; % your start value
years = 0; % keeps track of time
while moneys < 5100 % target amount
moneys = annualbalance(moneys);
years = years + 1;
end
fprintf('I have %f money after %f years\n', moneys, years);
Perhaps you could try to define an output variable and delare the variable (not the function) as persistent:
function annualbalance(x)
persistent out;
if isempty(out)
out = 0;
elseif out < 5000
out = out * 1.05
elseif out >= 5000
out = out * 1.10
end
That is not a good way to code it, however. The variable out will not be accessible outside the annualbalance function. Perhaps it would be better to work with an output variable. Also, your function receives x as an input argument, but x appears nowhere in the function. You should also check that out.
I know accessing something from a class is slower than a struct, but I'm looking for more info.
I'm writing a small gui that is going to give status updates as my program is running. Because messages are logged often in my code it actually slows down performance. What I did is implemented a buffer and only write the buffer to the gui whenever X messages have been logged. This seems to be doing ok, but I'm just curious about how matlab is put together.