Download script and run it

11 次查看(过去 30 天)
r.
r. 2017-11-2
I would like to have the following functionality:
websave('dummy.m', 'https://something.com/../coolscript.m');
disp(dummy(rand))
delete('dummy.m');
but without the temporary file.
Is it possible?
  2 个评论
Rik
Rik 2017-11-2
I don't think it is, unless you are going to use eval, and even then it depends on the script (as not all functions can be eval-ed). Why do you have a problem with the temporary file?
PS DON'T USE EVAL. Stephen Cobeldick is very pasionate about this topic (and with good reason), so I have taken the liberty of adapting some of his thoughts on this topic: Introspective programing (eval and other functions like it) is slow, and the MATLAB documentation warns specifically against it. Using eval is slow, makes debugging difficult, and removes all code helper tools (code checking, syntax hints, code completion, variable highlighting, etc.). A longer discussion can be found here. If you are not an expert and you think you need eval, there are probably better solutions to your problem.
r.
r. 2017-11-3
编辑:r. 2017-11-3
It would be cleaner to save the file to tempname, at least. But then how to best evoke the function? Unfortunately, run doesn't accept/return parameters, as far as I can tell; the next best option seems to be addpath / rmpath, which entails some string manipulation and checking.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2017-11-3
run is a script that you can look at. It finds the file name, cd's to the directory, runs the function, cd's back.
You can create a wrapper function to do the execution:
function varargout = runfunc(url, varargin)
if ~exist('url', 'var') || ~(ischar(url) || isstring(url)) || isempty(url)
error('bad url');
end
olddir = pwd;
cleanMe = onCleanup(@() cd(olddir) );
tfile = [tempname '.m'];
try
websave(tfile, url);
cleanMe2 = onCleanup(@() delete(tfile));
catch ME
rethrow(ME);
end
[tfilepath, tfilename, ext] = fileparts(tfile);
try
cd(tfilepath);
[varargout{1:nargout}] = feval(tfilename, varargin{:});
catch ME
rethrow(ME);
end
clear cleanMe2 cleanMe
end

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by