Use verstring
in the context of a try
\ catch
, the output in matlab will be an error while in Freemat it'll be something like "freemat 4.0", for example:
try
txt=verstring;
output='Freemat';
catch err
output='Matlab';
end
Question
I have some code which I need to run both in MATLAB and in Freemat, using different service functions in each case. To make the whole thing portable I need to find a way to determine which functions to call at the start of my code, depending on which environment I am in.
How does one do that?
I was thinking of using the version command but no too sure if this is really robust.
La solution
Use verstring
in the context of a try
\ catch
, the output in matlab will be an error while in Freemat it'll be something like "freemat 4.0", for example:
try
txt=verstring;
output='Freemat';
catch err
output='Matlab';
end
Autres conseils
I would try to identify each, and give an error if unsure, or ask the user to identify it manually in such case:
% try to identify if it is freeMat
isDefinatelyFreeMat = false;
try
versionIdentifier=verstring;
if (strcmpi(versionIdentifier(1:7), 'FreeMat'))
isDefinatelyFreeMat = true;
end
catch e
end
% try to identify if it is Matlab
isDefinatelyMatlab = false;
try
versionIdentifier=ver;
if (strcmpi(versionIdentifier.Name, 'Matlab'))
isDefinatelyMatlab = true;
end
catch e
end
% if identification was not successful
if ((isDefinatelyFreeMat && isDefinatelyMatlab) || (~isDefinatelyFreeMat && ~isDefinatelyMatlab))
error('Was unable to identify software.');
% TODO: Ask user to identify software manually
end