Assigning a function that does not exist to a new function?

1 Ansicht (letzte 30 Tage)
Romeo Acevedo
Romeo Acevedo am 30 Jul. 2018
Bearbeitet: Stephen23 am 31 Jul. 2018
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 :),

Antworten (2)

Aquatris
Aquatris am 30 Jul. 2018
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 Kommentare
Romeo Acevedo
Romeo Acevedo am 30 Jul. 2018
Thanks, Aquatris for your answer! Would you mind clarifying something for me further?
I am still confused on why CONTROLS is used as a class object later on (with all variables being declared as CONTROLS.x or CONTROLS.y .
Is this function declaration another way of declaring a class object?
Aquatris
Aquatris am 30 Jul. 2018
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 :)

Melden Sie sich an, um zu kommentieren.


Stephen23
Stephen23 am 30 Jul. 2018
Bearbeitet: Stephen23 am 31 Jul. 2018
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 Kommentare
Romeo Acevedo
Romeo Acevedo am 30 Jul. 2018
Bearbeitet: Romeo Acevedo am 30 Jul. 2018
Thanks, Stephen Cobeldick for your enlightening answer! There is still one aspect of this function that still confuses me.
You addressed that this function is also a structure; however, I thought that structures needed the keyword "struct" to have correctly Matlab syntax. Would you mind clarifying for me why is this case different?
Thanks again :)
Stephen23
Stephen23 am 31 Jul. 2018
Bearbeitet: Stephen23 am 31 Jul. 2018
"...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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Software Development Tools finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by