Class definition not updated after path change
조회 수: 5 (최근 30일)
이전 댓글 표시
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
답변 (1개)
per isakson
2018년 11월 9일
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
Christoph Hüsson
2023년 1월 26일
편집: per isakson
2023년 2월 4일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Write Unit Tests에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!