Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt_model is a handle-class, create independent copies #16

Closed
lordleoo opened this issue Feb 19, 2020 · 3 comments
Closed

opt_model is a handle-class, create independent copies #16

lordleoo opened this issue Feb 19, 2020 · 3 comments

Comments

@lordleoo
Copy link
Collaborator

the MOST object opt_model is a handle class
that is, if you say:
mdi2.om = mdi.om;
and change something on mdi2.om, you'll notice that the same change happened on mdi.om
that's because mdi2.om and mdi.om are just handles, that point MATLAB to the same 1 object in matlab's memory.
this can be annoying.

now consider this case:
you've already built mdi without solving it, i.e:

mdi0 = loadmd(....);
mdo0 = most(mdi0,mpoption(most_opts,'most.build_model',1,'most.solve_model',0));

after that, you did some changes on mdo0, (added user-defined functions and variables), and proceeded to build mdo1:

mdo1 = most(mdo0,mpoption(most_opts,'most.build_model',0,'most.solve_model',1));

After that, any changes you carry out on mdo1 are going to affect mdo0 as well
that's because inside MOST.m, for the case when most.build_model = 0, most.solve_model = 1;
we fixed that old bug by saying: om = mdi.om;

Mathworks introduced a new method for handle classes to allow creating independent duplicates of a handle class. This is only in recent versions of matlab. the command would look like:
mdi2.om = copy(mdi.om);

back to MOST.m code, instead of
om = mdi.om;
we can:
om = copy(mdi.om); to isolate the two objects.

using the copy(handle_class) method requires making a very small change on the class definition
in opt_model, instead of: classdef opt_model < handle
it should be: classdef opt_model < matlab.mixin.Copyable % this here was handle. handle classes can NOT be copied

@rdzman
Copy link
Member

rdzman commented Feb 19, 2020

Do you know in which version of MATLAB copy() and matlab.mixin.Copyable were introduced?

I'm guessing it does not work in Octave, but do you know if Octave has anything comparable?

Unfortunately, compatibility with relatively recent older MATLAB versions is important, and compatibility with Octave is critical.

@lordleoo
Copy link
Collaborator Author

This page here from Matlab documentation says it was introduced in 2011a

https://www.mathworks.com/help/matlab/ref/matlab.mixin.copyable.copy.html

I never used octave before.

@rdzman
Copy link
Member

rdzman commented Feb 27, 2020

It looks like Octave does not (yet) implement the matlab.mixin.Copyable class, according to bug 51317 in their bug tracker.

However, the opt_model constructor can copy an opt_model object, and that does work in Octave. So we can simply use that.

Also, I've included it a little later in the code so that the output object is always a copy of the input object.

@rdzman rdzman closed this as completed in 9f10f26 Feb 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants