Download script and run it
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
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.
Antworten (1)
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
0 Kommentare
Siehe auch
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!