Currently I have a function with settings/parameters for my program, where all parameters are written into a struct like "inp.month = 7;" so that I can pass the struct "inp" to functions afterwards. Is it possible to have all the assignments in the function without the struct, eg "month = 7;" and at the end somehow get 'all variables in this function' into the struct "inp". Something like "inp = <gather all variables assigned in this function>", and then have the struct be the output. It would add readability in my opinion and also I thought this to be easy, but didn't find a good solution yet and thought I'd ask here.
Cheers
Felix

댓글 수: 5

Matt J
Matt J 2021년 4월 26일
A better alternative is to automate the coding of the assignments with this:
Thanks! That is a good solution for when there are loads of variables. But also it only generates the code and does not automate the assignment itself. Also I already have it coded and just wanted to make it more concise.
Stephen23
Stephen23 2021년 4월 26일
"I thought this to be easy"
Nope. While there are a few approaches to achieve this, they will all add complexity and make your code slower.
This request is interesting only for lazy programmers who wants to make a quick and dirty code.
Just create a struct with the list of variables that you control explicitly.
Well, that might not be so obvious before thinking about it, eh?

댓글을 달려면 로그인하십시오.

 채택된 답변

Jeff Miller
Jeff Miller 2021년 4월 26일

0 개 추천

Maybe you would like something like this?
month = 1;
year = 1951;
day = 11;
inp = CreateStruct(month,day,year)
function outstruc = CreateStruct(varargin)
% Create a structure with the input variables as fields.
outstruc = struct;
for iField=1:numel(varargin)
sfName = inputname(iField);
outstruc.(sfName) = varargin{iField};
end
end

댓글 수: 2

Thanks, that does look interesting. Do you think there are any drawbacks or weird errors that could pop up while using this?
Stephen23
Stephen23 2021년 4월 27일
편집: Stephen23 2021년 4월 27일
"Do you think there are any drawbacks...?"
Slower code: "Avoid functions that query the state of MATLAB such as inputname, which, whos, exist(var), and dbstack. Run-time introspection is computationally expensive."

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2021년 4월 26일

0 개 추천

The following code is not recommended.. but it should work.
ZZ_filename = [tempname '.mat'];
save(ZZ_filename, '-struct', '-regexp', '^[^Z]', '^Z[^Z]');
inp = load(ZZ_filename);
delete(ZZ_filename);
If you already have variable names that begin with 'ZZ' then this code would have to be modified.
The purpose of the -regexp is to allow saving all of the variables except the over-head variable ZZ_filename .

댓글 수: 3

True, good point, that would work. I'd very much like to avoid saving the workspace to a file and reopening it.
Then you will need to write code that uses dynamic variable naming, which is not at all recommended.
Hint: who() returns names of variables.
Okay, thanks! I thought it might work without fiddling with the assignment of variables myself (eg with a function written exactly for this), but it might not.

댓글을 달려면 로그인하십시오.

Bruno Luong
Bruno Luong 2021년 4월 27일
s = buildparams()
s = struct with fields:
a: 1 b: 2
function s = buildparams()
a = 1;
b = 2;
s = gathervars();
end
function s = gathervars()
vars = evalin('caller', 'whos');
for k=1:length(vars)
name = vars(k).name;
s.(name) = evalin('caller', name);
end
end

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 4월 26일

댓글:

2021년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by