MATLAB Answers

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

9 ビュー (過去 30 日間)
Jim Svensson
Jim Svensson 2021 年 10 月 24 日
編集済み: Jim Svensson 2021 年 10 月 24 日
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 件のコメント
Jim Svensson
Jim Svensson 2021 年 10 月 24 日
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.

サインインしてコメントする。

回答 (1 件)

Matt J
Matt J 2021 年 10 月 24 日
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 件のコメント
Jim Svensson
Jim Svensson 2021 年 10 月 24 日
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.

サインインしてコメントする。

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by