Skip to content

Commit

Permalink
feat: ✨ Add utilities to analyse MATLAB structs and workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
djmaxus committed Mar 21, 2024
1 parent 7a673f0 commit 4e81461
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
26 changes: 26 additions & 0 deletions util/struct_diff.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function [result_new, result_base, upd_base] = struct_diff(struct_new, struct_base)
fields_new = fieldnames(struct_new);
fields_base = fieldnames(struct_base);

new_diff_base = setdiff(fields_new, fields_base);
base_diff_new = setdiff(fields_base, fields_new);
assert(numel(base_diff_new)==0);

result_new = struct();

for i = 1:numel(new_diff_base)
result_new.(new_diff_base{i}) = struct_new.(new_diff_base{i});
end

result_base = struct_base;
upd_base = struct();

field_intersect = intersect(fields_new, fields_base);
for i = 1:numel(field_intersect)
if ~isequal(struct_new.(field_intersect{i}), struct_base.(field_intersect{i}))
upd_base.(field_intersect{i}) = struct_new.(field_intersect{i});
result_base = rmfield(result_base, field_intersect{i});
warning('base struct field updated and placed in the new struct')
end
end
end
11 changes: 11 additions & 0 deletions util/workspace_pack.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function [packed] = workspace_pack()

names = evalin('caller', 'who');

packed = struct();
for i = 1:length(names)
varName = names{i};
packed.(varName) = evalin('caller', varName);
end

end
10 changes: 10 additions & 0 deletions util/workspace_unpack.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function [] = workspace_unpack(packed)

names = fieldnames(packed);

for i = 1:length(names)
varName = names{i};
assignin('caller', varName, packed.(varName));
end

end

0 comments on commit 4e81461

Please sign in to comment.