Comparing changes in value of input variable to function for every function call

1 回表示 (過去 30 日間)
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 件のコメント
Walter Roberson
Walter Roberson 2022 年 8 月 25 日
where do you assign previous_settings_file?
Christoffer Sørensen
Christoffer Sørensen 2022 年 8 月 25 日
編集済み: Walter Roberson 2022 年 8 月 25 日
That’s one of my issues. I’m not sure how to define. Because the first time I call the function there is no previous value. It only the second time and onwards it should be a struct. I Have tried to follow this example: https://www.mathworks.com/matlabcentral/answers/638780-how-to-compare-previous-and-next-values-in-matlab
But I’m not sure whether it’s useful for what I want to do.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2022 年 8 月 25 日
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;
  1 件のコメント
Christoffer Sørensen
Christoffer Sørensen 2022 年 8 月 26 日
This looks promising! I looked into the documentation of the "persistent" function and I think thats what I need! I will try to test it when im back at work.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by