Download script and run it

9 Ansichten (letzte 30 Tage)
r.
r. am 2 Nov. 2017
Beantwortet: Walter Roberson am 3 Nov. 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 Kommentare
Rik
Rik am 2 Nov. 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. am 3 Nov. 2017
Bearbeitet: r. am 3 Nov. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 3 Nov. 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

Kategorien

Mehr zu Downloads finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by