create a method which checks if a variable changed after a complete new start of the programm in matlab

A possible way to verify if a variable changed after a program was restarted is to save the variable in a file at the end of program execution and compare its contents at the beginning of the next execution. Here is an example of how to create such a method in MATLAB:

main.m
function is_changed = is_variable_changed(var_name)
% Checks if a variable changed after a complete new start of the program
% Inputs:
%   var_name: name of the variable to check
% Outputs:
%   is_changed: logical flag indicating if the variable has changed (true),
%               or not (false)

% Get the file path where the variable is saved
file_path = fullfile(tempdir, [var_name, '.mat']);

% Check if the file exists (i.e., if the program was already executed before)
if exist(file_path, 'file') == 2
    % Load the variable saved in the previous execution
    loaded_var = load(file_path, var_name);
    loaded_var = loaded_var.(var_name);
    
    % Compare the loaded variable with the current one
    current_var = evalin('caller', var_name);
    is_changed = ~isequal(loaded_var, current_var);
else
    % If the file does not exist, there is no previous execution, so the
    % variable has not changed
    is_changed = false;
end

% Save the current value of the variable for the next execution
save(file_path, var_name);
end
1040 chars
30 lines

To use this method, you just need to call it passing the name of the variable you want to check, like this:

main.m
variable_to_check = [1 2 3];
changed = is_variable_changed('variable_to_check');
% Do some processing...
variable_to_check = [4 5 6];
changed = is_variable_changed('variable_to_check');
186 chars
6 lines

In the example above, the first call to is_variable_changed will return false because there was no previous execution of the program, and the variable variable_to_check was just created. The second call will return true because the variable was updated, and different from the one saved in the previous execution.

related categories

gistlibby LogSnag