function paramValue = getVarFromGlobalContext(modelH, paramName)
%% Fetches parameter value from the global context. i.e. base workspace/data
% dictionary, MAT files
% Input arguments:
% modelH - Handle to model
% paramName - Parameter whose value needs to be fetched from the global
% context. Can be variables/struct/bus

% Note: Using getVarFromGlobalScope(), we can currently only
% fetch the top level object from the global context, i.e.
% if we are interested in x.y.z, where x is defined in the
% global context, we need to fetch x first from the global
% context, then reference into the returned value to get
% x.y.z
% We may also choose to use resolveInGlobalScope() for fetching fields of
% structures/Simulink Parameters to see resolved values.
% e.g: if the model uses Simulink Parameter object carTopLvl with the
% structure:
% carTopLvl
%   - Value: 
%       - EngineBus
%          - speed.
%   - <OtherMetadataFields>
% If we want to query the value of the speed subparameter in EngineBus,
% i.e. the 'paramName' argument passed into this function would be
% 'carTopLvl.Value.EngineBus.speed', we could ALSO do:
% >> engineBusObj = resolveInGlobalScope(modelH, 'carTopLvl')
% >> param_value = engineBusObj.speed
parameterNames = split(paramName, '.');
if numel(parameterNames) == 1
    paramValue = getVarFromGlobalScope(modelH, paramName);
else
    % Get top level parameter object
    paramValue = getVarFromGlobalScope(modelH, parameterNames{1});
    % Iterate over sub-properties of topLevelObj to find
    % the one we want.
    for idx=2:numel(parameterNames)
        paramValue = paramValue.(parameterNames{idx});
    end
end
end