उपयोग run
, यह आप अपने खुद के कार्यों के लिए उन्हें पथ को जोड़ने बिना बनाया-इन के बजाय का उपयोग करने की अनुमति देगा।
मदद से लिया:
स्क्रिप्ट चलाएं कि वर्तमान पथ पर नहीं है सिंटेक्स
रन SCRIPTNAME स्क्रिप्ट
@Cheery के रूप में सही ढंग से कहा, यह काम करता है कि स्वीकार करने के लिए नहीं किया जा सकता तर्क। हालांकि, run.m
संशोधित फ़ाइल है, इसलिए मैंने एक विस्तारित संस्करण बनाया है, जो तर्क स्वीकार कर सकता है। इसे आउटपुट तर्कों के साथ-साथ आसानी से संशोधित किया जा सकता है।
function runExtended(script,varargin)
cur = cd;
if isempty(script), return, end
if ispc, script(script=='/')='\'; end
[p,s,ext] = fileparts(script);
if ~isempty(p),
if exist(p,'dir'),
cd(p)
w = which(s);
if ~isempty(w),
% Check to make sure everything matches
[wp,ws,wext] = fileparts(w);
% Allow users to choose the .m file and run a .p
if strcmp(wext,'.p') && strcmp(ext,'.m'),
wext = '.m';
end
if ispc
cont = ~strcmpi(wp,pwd) | ~strcmpi(ws,s) | ...
(~isempty(ext) & ~strcmpi(wext,ext));
else
cont = ~isequal(wp,pwd) | ~isequal(ws,s) | ...
(~isempty(ext) & ~isequal(wext,ext));
end
if cont
if exist([s ext],'file')
cd(cur)
rehash;
error('MATLAB:run:CannotExecute','Can''t run %s.',[s ext]);
else
cd(cur)
rehash;
error('MATLAB:run:FileNotFound','Can''t find %s.',[s ext]);
end
end
try
feval(s,varargin{:});
% evalin('caller', [s ';']);
catch e
cd(cur);
rethrow(e);
end
else
cd(cur)
rehash;
error('MATLAB:run:FileNotFound','%s not found.',script)
end
cd(cur)
rehash;
else
error('MATLAB:run:FileNotFound','%s not found.',script)
end
else
if exist(script,'file')
evalin('caller',[script ';']);
else
error('MATLAB:run:FileNotFound','%s not found.',script)
end
end
end
पुस्तकालय कितना बड़ा है, और आप इसके साथ कितना काम करने जा रहे हैं? आप अपने कार्यों से कितनी बार कॉल करना चाहते हैं? पुस्तकालय में ओओ कोड है? –