How can I vary a value in both the main script and a function file?

I have a system of 4 nonlinear ODE's which I have in a function file along with the parameters. One of which is the parameter 'K'.
The same parameter K is also in the main script. Both of these K's need to be the same for the script to run. If I have both equal to say K=0.198 I get sensible results
I'd like the value of K to increase from 0 to 0.2 in small steps (say 0.05) in the function but I also need the K value in the main script to change at exactly the same time.
Will I need to vary K in the function file and have the script call it in somehow from it?
Any help would be appreciated! I can provide the code but it's long and messy.
Thanks!
Calum

Answers (1)

You could create a simple handle class that has some value. Then pass the handle into the function and whenever the property is updated the new value will be accessible.
You could even have it notify listeners if you wanted so that the value gets updated completely automagically.
For example:
classdef K < handle
properties
value
end
methods
function obj = K(val)
obj.value = val;
end
end
end
Now
x = K(pi)
Pass x into the function.
Then if you set the value to something new, say:
x.value = exp(1)
Now you when you reference x.value, it will be upated:
y = x.value*pi;

This question is closed.

Asked:

on 2 Jul 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!