You can use exist function for this process along with if else statements to achieve your goal in .m file:
Refer to exist documentation. This is a example of the same for checking if a variable is in workspace or not.
% if variable exists in the workspace
if exist('a') == 0
disp(0)
% if variable doesn't exists in the workspace
elseif exist('a') == 1
disp(1)
end
The same can be checked for files as well.
Hope it helps.