Download script and run it

14 visualizaciones (últimos 30 días)
r.
r. el 2 de Nov. de 2017
Respondida: Walter Roberson el 3 de Nov. de 2017
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 comentarios
Rik
Rik el 2 de Nov. de 2017
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. el 3 de Nov. de 2017
Editada: r. el 3 de Nov. de 2017
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.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 3 de Nov. de 2017
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

Categorías

Más información sobre Downloads en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by