Comparing changes in value of input variable to function for every function call
3 views (last 30 days)
Show older comments
Christoffer Sørensen
on 25 Aug 2022
Commented: Christoffer Sørensen
on 26 Aug 2022
Hi!
I have some issues making a routine that can compare an input variable in a new function call with the variable of the same name from a previous function call. The input variable is a struct for which the fields have been changed via a GUI. I need to compare the structs for every function call and track the changes in the fields to pass the changed field values on to other functions.
here is my code:
function Equipment_calls(Settings_file,Equipment_list,Equipment_libs)
%Comparing the each succesive settings file with the previous one to check
%which field has changed and using that so send the command further down
%the hierachy
% THE LINES BELOW IS MY ATTEMPT TO MAKE THE FEATURE COMPARING THE VARIABLE/STRUCT FROM THE
% PREVIOUS FUNCTION CALL WITH THE VARIABLE/STRUCT FROM THE CURRENT FUNCTION
% CALL
new_settings_file = Settings_file;
while 1
if exist('previous_settings_file','var') ~= 1 || isequal(new_settings_file,previous_settings_file)
fprintf('No changes to the settings file');
else
[~, ia, ib] = setdiff(new_settings_file, previous_settings_file);
disp(Settings_file(ia))
disp(Settings_file(ib))
end
%THE FOLLOWING LINES HANDLES THE FIELDS IN THE STRUCT
Settings_field = fieldnames(Settings_file);
%Code handling all the changed fields in the structs.
for i = 1:length(Settings_field)
if contains(Settings_field(i),'functions')
field = Settings_field(i);
fieldname = sprintf('%s',string(field));
if Settings_file.(fieldname).initialise_shutdown == 1
%Initialising equipment which correspond to the "Equipment_list"
switch fieldname
case 'Generator_functions'
idx = find(contains(Equipment_list.EquipmentType,'Generator'));
Libpath = Equipment_libs{idx};
init_file = dir(join([Libpath,'\*init.m']));
cmd_file_string = dir(join([Libpath,'\*cmd.m']));
cmd_file_string = cmd_file_string.name;
cmd_file_string = erase(cmd_file_string,'.m');
cmd_func = str2func(cmd_file_string);
run(init_file.name)
feval(cmd_func,Settings_file);
case 'Scope_functions'
idx = find(contains(Equipment_list.EquipmentType,'Scope'));
Libpath = Equipment_libs{idx};
init_file = dir(join([Libpath,'\*init.m']));
cmd_file_string = dir(join([Libpath,'\*cmd.m']));
cmd_file_string = cmd_file_string.name;
cmd_file_string = erase(cmd_file_string,'.m');
cmd_func = str2func(cmd_file_string);
run(init_file.name)
feval(cmd_func,Settings_file);
case 'Stage_functions'
idx = find(contains(Equipment_list.EquipmentType,'Stage'));
Libpath = Equipment_libs{idx};
init_file = dir(join([Libpath,'\*init.m']));
run(init_file.name)
end
elseif Settings_file.(fieldname).initialise_shutdown == 0
%Closing equipment which correspond to the "Equipment_list"
switch fieldname
case 'Generator_functions'
idx = find(contains(Equipment_list.EquipmentType,'Generator'));
Libpath = Equipment_libs{idx};
close_file = dir(join([Libpath,'\*close.m']));
run(close_file.name)
case 'Scope_functions'
idx = find(contains(Equipment_list.EquipmentType,'Scope'));
Libpath = Equipment_libs{idx};
close_file = dir(join([Libpath,'\*close.m']));
run(close_file.name)
case 'Stage_functions'
idx = find(contains(Equipment_list.EquipmentType,'Stage'));
Libpath = Equipment_libs{idx};
close_file = dir(join([Libpath,'\*close_y.m']));
run(close_file.name)
end
else
%Handling all other settings changes or calls to functions
switch fieldname
case 'Generator_functions'
idx = find(contains(Equipment_list.EquipmentType,'Generator'));
Libpath = Equipment_libs{idx};
function_file = dir(join([Libpath,'insert changed field name here']));
case 'Scope_functions'
idx = find(contains(Equipment_list.EquipmentType,'Scope'));
Libpath = Equipment_libs{idx};
function_file = dir(join([Libpath,'insert changed field name here']));
case 'Stage_functions'
idx = find(contains(Equipment_list.EquipmentType,'Stage'));
Libpath = Equipment_libs{idx};
function_file = dir(join([Libpath,'insert changed field name here']));
end
end
end
end
The 'Settings_file' input contains the structs that are subject to change via the GUI.
Hope it makes sense
Best regards
Christoffer
2 Comments
Accepted Answer
Walter Roberson
on 25 Aug 2022
function Equipment_calls(Settings_file,Equipment_list,Equipment_libs)
%Comparing the each succesive settings file with the previous one to check
%which field has changed and using that so send the command further down
%the hierachy
% THE LINES BELOW IS MY ATTEMPT TO MAKE THE FEATURE COMPARING THE VARIABLE/STRUCT FROM THE
% PREVIOUS FUNCTION CALL WITH THE VARIABLE/STRUCT FROM THE CURRENT FUNCTION
% CALL
persistent previous_settings_file
new_settings_file = Settings_file;
if isempty(previous_settings_file); previous_settings_file = new_settings_file; end
while true
if isequal(new_settings_file,previous_settings_file)
fprintf('No changes to the settings file');
else
[~, ia, ib] = setdiff(new_settings_file, previous_settings_file);
disp(Settings_file(ia))
disp(Settings_file(ib))
end
previous_settings_file = new_settings_file;
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!