Can you update a constant in a function.

16 次查看(过去 30 天)
I currently have 3 functions which rely on a constant I have created inside a file called cConstants.m
classdef cConstants
properties (Constant)
a = 3
m = 1
end
end
The 3 functions which this constant are myv.m, mydv.m and myd2v.m
function [ v ] = myv( x )
import constants.*;
v = 2*exp(-(x^2)/2)+(cConstants.m/(cConstants.m+(x-cConstants.a)^2));
end
With mydv.m and myd2v.m being the first and second derivative of myv.m
I have a function which finds the minimum points of myv.m.
I now want to create a function which finds the minimum points of myv.m as the constant cConstants.a varies over an interval.
Something similar to this.
function f = PlotMin(a,b,h) %%a = starting value of a, b = end value of a and h is increase interval
hold on
while a <= b
cConstants.a = a;
x = FindMin(-10,10,0.5,4);
y = mydv( x );
plot(x,y)
a = a+h;
end
  4 个评论
Adam
Adam 2018-1-22
You can set a default value for any property so if you do that and never change it then it would act like a constant from a usage perspective, but it gives the additional ability to alter it if you need to, which can be good and bad since it allows for accidental changes that a constant doesn't, but works for what you want with deliberate changes.
Lee Luderman
Lee Luderman 2018-1-22
How would I go about defining this property across multiple function files?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-1-22
"Use constant properties to define constant values that you can access by name. Create a class with constant properties by declaring the Constant attribute in the property blocks. Setting the Constant attribute means that, once initialized to the value specified in the property block, the value cannot be changed."
  1 个评论
Lee Luderman
Lee Luderman 2018-1-22
Thank you for the prompt reply, what options are there to achieve what I'm looking to do?

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2018-1-22
By definition a constant property is ... constant. Matlab is not designed so that these properties can change.
To be perfectly honest, it is possible to design the class so that you could change the constant properties but that would involve having to call clear classes each time you want to update the constant values. This could have some unpleasant side effects.
The workaround would be to create a singleton non-constant class. You'd be in effect creating a class that acts as a global so I'm not sure it is a good idea either.
I think you'd be better off having a normal class with non-constant properties and pass instances of the class to your mydv and co. functions.
  5 个评论
Guillaume
Guillaume 2018-1-22
编辑:Guillaume 2018-1-22
The getGlobalx functions are a bit pointless. They don't give any kind of safety above just fetching the global directly.
Using globals (or my singleton class emulating globals) is really not recommended (in any language). It really makes it hard to debug code when any function anywhere can change the value of a variable. It's a maintenance nightmare when you have to review every single line of code in every function when you edit a global variable and have to check which piece of code it will affect.
The proper way to do what you want is to pass these not really constant anymore values to the function, as proper arguments.
Kevin
Kevin 2021-10-7
Thank you so much for your explanation, this problem has troubled me for a long time.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by