function mapath = getpmpath(folder,MXPversion)
% path to folders SimFiles, Materials, DXF, etc.

% if isdeployed    % if run from standalone version

% Read item from Windows registry
% valnames = winqueryreg('name','rootkey','subkey')
% value = winqueryreg('rootkey','subkey','valname')
% value = winqueryreg('rootkey','subkey')


if ~exist('MXPversion','var') || isempty(MXPversion)
    if isdeployed
        MXPversion='standalone';
    else
        MXPversion='matlab';
    end
end

if strcmp(MXPversion,'standalone')
    % standalone version
    try
        mapath = winqueryreg('HKEY_CURRENT_USER','Software\VepcoTech\MotorXP-PM','RootPath');
    catch
        errordlg('Cannot find Windows registry key ''RootPath'' in ''HKEY_CURRENT_USER\Software\VepcoTech\MotorXP-PM''','Error','modal');
        pause(10);
    end    
elseif strcmp(MXPversion,'matlab')
    % matlab version
    try 
        mapath = winqueryreg('HKEY_CURRENT_USER','Software\VepcoTech\MotorXP-PM','RootPathMatlab');
        if ~exist([mapath '\Circuits'],'dir') || ~exist([mapath '\Materials'],'dir') || ~exist([mapath '\SimFiles'],'dir') || ~exist([mapath '\SimScripts'],'dir')
            error(' ');   % go to catch
        end
    catch
        mapath = cd;
        system(['REG ADD HKCU\Software\VepcoTech\MotorXP-PM /v RootPathMatlab /t REG_SZ /d ' mapath ' /f']);
    end
end
mapath = strrep(mapath,'\','/');
mapath = [mapath '/' folder];

