Matlab does not find new method in class folder without "clear classes"

22 visualizzazioni (ultimi 30 giorni)
I want to understand if this is intended behavior or dependes on something specific to my setup.
I have a class in a class folder such as @world_t/word_t.m. I also have one or more methods in separate files in said class folder @world_t. I have a program that instantiates an object and runs some methods, it works.
Now if I add a new method "run_sim" by adding a file @world_t/run_sim.m and modify the program to call that method, it does not work. The error is "Unrecognized method, property, or field 'run_sim' for class 'world_t'.".
If I do "clear classes" and run the program again it works. So apparently Matlab does not see the new method added when the old class is already loaded into memory, as can be seen by "inmem()" call.
This is on Windows 10 and a local file system on "C:\...".
Is this expected behaviour? Is there a better way to make Matlab see the new method without "clear classes"?
(One problem with clear classes is that it also clears global variables, and I have one that I want to keep.)
  2 Commenti
Matt J
Matt J il 24 Ott 2021
Modificato: Matt J il 24 Ott 2021
One problem with clear classes is that it also clears global variables, and I have one that I want to keep.
Highly inadvisable...
Jim Svensson
Jim Svensson il 24 Ott 2021
Modificato: Jim Svensson il 24 Ott 2021
I found a nicer solution, to protect the global variable with mlock. Then I can do "clear classes". Also, it is actually a persisten variable in a function, if that feels better, not a global. But it is the same from from "clear classes" perspective.

Accedi per commentare.

Risposta accettata

Graeme Yeo
Graeme Yeo il 29 Nov 2023
This post is a couple of years old now, but this might be useful to someone still looking for an answer...
It seems that in order for the new method to be made visible automatically (without clearing instances of the class from the workspace), it must be added directly to the main class .m file. Obviously the point is that we want the function in a separate file in the class folder, but you can actually add function stubs to your main class file (similar to function prototypes in C++ etc).
So in the OP's example:
Edit @world_t/world_t.m and add the following:
classdef world_t
properties
...
end
methods
%Function stubs
[outputArg1, outputArg2] = run_sim(obj, inputArg1, inputArg2); %Function stub for run_sim function
...
end
end
The function stub should be the same as the first line of the function file, but without the 'function' keyword.
Adding the stub forces the class to get automatically updated to include the new function straight away.
As a side point, when searching for Matlab documentation about using function stubs, I couldn't actually find any reference to them any more, but I was sure they existed, and looked at some of my old code where I did indeed use them. I suppose they are now largely surplus to requirements and just add more lines of code that aren't really needed, hence being removed from the documentation.
Hope this helps someone.
  4 Commenti
Matt J
Matt J il 29 Nov 2023
Modificato: Matt J il 29 Nov 2023
I found this only happens when you add a new method or property to the class file. If you just add whitespace or a comment, or even modify the code in a function defined in the class file, it does not cause the auto-update to happen.
So, I guess the workaround would be to create some fake, commented-out property in the main class file. Every time you want to update the class, you could:
  1. Uncomment the property.
  2. Save.
  3. Re-comment it.
  4. Save.
Jim Svensson
Jim Svensson il 18 Apr 2024
Spostato: Matt J il 19 Apr 2024
Quite annoying that Matlab cannot automatically detect if a new method file is. I am sure there are functions in Windows files sytem API to monnitor folders for changes, at least on a local file system.

Accedi per commentare.

Più risposte (1)

Matt J
Matt J il 24 Ott 2021
I don't know if it's intended or not, but my experience is that it only happens when your class is in a @-directory. If you put everything in a single classdef file, it doesn't happen. Also, you might be able to avoid clearing the entire workspace if you do instead,
clear world_t
  1 Commento
Jim Svensson
Jim Svensson il 24 Ott 2021
Yes, that is my experience too. In the real situation I have many classes and many methods. To keep them in one classfile is not viable.

Accedi per commentare.

Categorie

Scopri di più su Software Development Tools in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by