create a method which checks if a variable has changed after another run in matlab

You can create a custom method in MATLAB that uses persistent variables to store the previous value of the variable being checked.

Here's an example:

main.m
function isChanged = checkIfChanged(currentValue)
%# This function returns true if the currentValue is different 
%# from the last storedValue

persistent storedValue
if isempty(storedValue)
    storedValue = currentValue;
    isChanged = false; %# Initialize to false
else
    isChanged = ~isequal(currentValue, storedValue);
    storedValue = currentValue;
end
363 chars
13 lines

The persistent keyword declares that storedValue is a variable that keeps its value between different calls of the function.

To use this function, just call it with the variable you want to check:

main.m
x = [1, 2, 3];
checkIfChanged(x) % False, because it's the first call

x(1) = 4;
checkIfChanged(x) % True, because the first element has changed
145 chars
6 lines

Note that the isequal function is used to test for equality between the currentValue and storedValue variables, so any change in the variable will trigger the isChanged output to be true. You can modify this comparison to fit your specific needs.

related categories

gistlibby LogSnag