Can you update a constant in a function.

조회 수: 19 (최근 30일)
Lee Luderman
Lee Luderman 2018년 1월 22일
댓글: Kevin 2021년 10월 7일
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