Using matlab 2010 with NET.addAssemb开发者_如何转开发ly(MyName), how do I check whether certain assembly MyName was already added or not?
I've found a code which checks for added assembly. Maybe I can alter it to something like
function flag = IsAssemblyAdded( MyName )
domain = System.AppDomain.CurrentDomain;
assemblies = domain.GetAssemblies;
flag = false;
for i= 1:assemblies.Length
asm = assemblies.Get(i-1);
disp(char(asm.FullName));
if strcmpi(asm.FullName, MyName)
flag = true;
end
end
for checking whether the assembly is loaded to matlab or not.
[edited]
try using isempty(which(MyName))
- I believe that if MyName is not defined/loaded then this will be empty (i.e., true), otherwise not (false).
The following works in R2013a and looks for the assembly short name:
function loaded = IsAssemblyAdded( MyName )
assembiles = System.AppDomain.CurrentDomain.GetAssemblies;
loaded = false;
i = 0;
while (i < assembiles.Length && ~loaded)
loaded = strcmp(char(assembiles.Get(i).GetName.Name), MyName);
i = i + 1;
end
Straight strcmp may not work (at least as of 2011b) as the FullName method will return a much longer string. E.g:
"mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
Something like this might work better:
regexp(char(asm.FullName),['^', MyName, ','],'once')
This works:
asm = System.AppDomain.CurrentDomain.GetAssemblies;
any(arrayfun(@(n) strncmpi(char(asm.Get(n-1).FullName), MyName, length(MyName)), 1:asm.Length))
精彩评论