Created
March 10, 2024 13:48
-
-
Save ahartlba/eb63583d27c2f3d33990bf8f2ba0c596 to your computer and use it in GitHub Desktop.
Pack variables from current workspace into struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function s = packs(varargin) | |
% pack struct | |
% if no arguments are passed in ... whole workspace gets packed | |
% else only arguments get parsed. | |
% | |
% Example: | |
% | |
% a = 4; | |
% b = 3; | |
% c = 15; | |
% pack everything: | |
% s = packs() % s.a = 4; s.b = 3; s.c = 15; | |
% select packing: | |
% s = packs(a, b) % s.a = 4; s.b = 3; | |
% workspace that called this function | |
eval_ws = evalin('caller', 'whos'); | |
% get input-names of spack | |
if nargin == 0 | |
for i=1:length(eval_ws) | |
s.(eval_ws(i).name) = evalin('caller', eval_ws(i).name); | |
end | |
else | |
for i=1:nargin | |
s.(inputname(i)) = varargin{i}; | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment