Assigning a function that does not exist to a new function?
2 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I new to Matlab and I am trying to understand the implementation of the following program.
In this program function CONTROLS is taking the value of SB2_ControlSettings; however, SB2_ControlSettings is not defined anywhere. The name of the file is SB2_ControlSettings.m though. Then the program goes about using every variable of this newly created function and use it as members of a class.
% SB2_CONTROLSETTINGS Set parameters to control the SPARSEBAYES algorithm
function CONTROLS = SB2_ControlSettings
%%Define parameters which influence the underlying operation of the
%%SparseBayes inference algorithm
% TOLERANCES
%
% Any Q^2-S "relevance factor" less than this is considered to be zero
%
CONTROLS.ZeroFactor = 1e-12;
%
% If the change in log-alpha for the best re-estimation is less than this,
% we consider termination
%
CONTROLS.MinDeltaLogAlpha = 1e-3;
If anybody could help me, I would highly appreciate it.
Have a wonderful day :),
0 个评论
回答(2 个)
Aquatris
2018-7-30
In Matlab functions are defined as
function y = funName(x)
which takes the input x and outputs y. So in the pasted code, the function is called "SB2_ControlSettings" and does not take any input. The output is the "Controls" variable. I believe this function is just to adjust settings for further in the code.
2 个评论
Aquatris
2018-7-30
In Matlab the "CONTROLS" variable is a structure variable, not a class. The structure variables are usually preferred when you want to store several related variables within a single variable.
Keep in mind this is not C language and do not assume the syntax is the same :)
Stephen23
2018-7-30
编辑:Stephen23
2018-7-31
Your misunderstand several fundamental aspects of MATLAB functions and variable assignment.
"In this program function CONTROLS is taking the value of SB2_ControlSettings;"
No, The function you have shown is actually SB2_ControlSettings. It returns one output argument, which is named Controls. How to define functions is clearly explained in the documentation:
"however, SB2_ControlSettings is not defined anywhere."
The name of the function is always defined on the line with function, just like with your code.
"The name of the file is SB2_ControlSettings.m though."
Yes, and that is the name of the function. These should be the same (and I highly recommend that they are).
"Then the program goes about using every variable of this newly created function and use it as members of a class."
No, there is no class being defined here. The variable Controls is a structure:
The structure Controls does not exist at the start of the function, but it is implicitly created by the first time it is allocated to.
2 个评论
Stephen23
2018-7-31
编辑:Stephen23
2018-7-31
"...I thought that structures needed the keyword "struct" to have correctly Matlab syntax."
Using struct is just one way of creating a structure. The documentation shows that it is possible to implicitly create a structure simply by allocating to it:
"Would you mind clarifying for me why is this case different?"
The answer to your question is "by definition": because that is what MATLAB is designed to do.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Software Development Tools 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!