Assigning a function that does not exist to a new function?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (2)
Aquatris
il 30 Lug 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 Commenti
Aquatris
il 30 Lug 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 :)
Stephen23
il 30 Lug 2018
Modificato: Stephen23
il 31 Lug 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 Commenti
Stephen23
il 31 Lug 2018
Modificato: Stephen23
il 31 Lug 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.
Vedere anche
Categorie
Scopri di più su Software Development Tools in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!