Class definition not updated after path change
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I'm having trouble with class definition update when I change Matlab path within a script.
Let's illustrate with a simple exemple.
I have these folders in my workspace, none of them are in the Matlab path.
The myClass class is as simple as it can be. The one in the FolderA is defied as below :
classdef myClass
properties
FolderAprop
end
end
And the one in FolderB :
classdef myClass
properties
FolderBprop
end
end
When I change the Matlab path to use the FolderB class instead of Forlder A class within a script, the class definition doesn't updates if one instance of the class is in the workspace. The code below, all within a script called testClassUpdate.m, shows it :
clear all
addpath ./FolderA
which myClass
myClassInstA = myClass;
properties(myClassInstA)
rmpath ./FolderA
addpath ./FolderB
which myClass
myClassInstB = myClass;
properties(myClassInstB)
rmpath ./FolderB
It returns :
>> testClassUpdate
L:\tmp\TestPathClass\FolderA\@myClass\myClass.m % myClass constructor
Properties for class myClass:
FolderAprop
L:\tmp\TestPathClass\FolderB\@myClass\myClass.m % myClass constructor
Properties for class myClass:
FolderAprop
But when I execute the exact same code in two times, returning the the prompt after addpath ./FolderB, it works as expected :
>> clear all
addpath ./FolderA
which myClass
myClassInstA = myClass;
properties(myClassInstA)
rmpath ./FolderA
addpath ./FolderB
L:\tmp\TestPathClass\FolderA\@myClass\myClass.m % myClass constructor
Properties for class myClass:
FolderAprop
>> which myClass
myClassInstB = myClass;
properties(myClassInstB)
rmpath ./FolderB
L:\tmp\TestPathClass\FolderB\@myClass\myClass.m % myClass constructor
Properties for class myClass:
FolderBprop
I tried to call the rehash function but it has no effect. If I clear all instances of myClass after changing path, it works as expected but I try to avoid that.
I read the two following help pages and, if I understand correctly, the class definition should have been updated as I put my class files in @ folders.
Do anyone knows how to get my class definition updated correctly ?
Regards,
Julien
0 Kommentare
Antworten (1)
per isakson
am 9 Nov. 2018
I have reproduced the behavior on R2018a.
The following script
%%testClassUpdate
clear all
addpath ./FolderA
[~] = which('myClass');
myClassInstA = myClass;
disp( properties(myClassInstA) )
clear myClass
rmpath ./FolderA
addpath ./FolderB
[~] = which('myClass');
myClassInstB = myClass;
disp( properties(myClassInstB) )
rmpath ./FolderB
When I run the entire script in one go (F5) it displays
'FolderAprop'
'FolderAprop'
However, when I run it section by section (Cntrl+Shift+Enter) it displays
'FolderAprop'
'FolderBprop'
I get identical results even without the clear myClass
IMO: This is a case for Tech Support.
1 Kommentar
Christoph Hüsson
am 26 Jan. 2023
Bearbeitet: per isakson
am 4 Feb. 2023
i ran into the same issue. you need to clear existing variables that use type-definitions you are about to update. read this, especially the section "Changing Path to Update Class Definition":
I am using sth like this to remove only those workspace variables, which would potentially be affected:
currVars = evalin('base', 'whos');
zimVars = {};
for v = 1:length(currVars)
if startsWith(currVars(v).class, 'ZimV.')
zimVars{end + 1} = currVars(v).name;
end
end
if ~isempty(zimVars)
if ~NameValueArgs.keepConflictingVaraibles
% https://stackoverflow.com/questions/20167762/evalin-with-variable-matlab
% required because we are going to remove them from inside a evalin-call
assignin('base', 'zimVars', zimVars);
warning('Removing variables from your workspace that require classes of this package. If you do NOT want this for future calls, use keepConflictingVaraibles=true')
evalin('base', 'clear(zimVars{:}); clear zimVars');
else
warning('There are variables in your workspace that require updates to class-definitions of this package. You might experience unexpected behavior, as existing variables might use stale definitions. Read more about this here (especially section "Changing Path to Update Class Definition"): https://de.mathworks.com/help/matlab/matlab_oop/organizing-classes-in-folders.html')
end
end
Siehe auch
Kategorien
Mehr zu Write Unit Tests finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!