Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to delete a MATLAB script from within itself?

Tags:

matlab

In MATLAB, we could place the following inside of a script named me.m.

delete('me.m');

After we run the script, it removes itself. Is such a thing safe in MATLAB?

like image 448
Joe C Avatar asked Apr 14 '16 14:04

Joe C


People also ask

How do you delete a script in MATLAB?

To clear all variables from the current workspace, use clear or clearvars . To clear all global variables, use clear global or clearvars –global . To clear a particular class, use clear myClass . To clear a particular function or script, use clear functionName .

How delete all files in MATLAB?

Select MATLAB > General and in the Deleting files section, select from the available options. Alternatively, you can use the recycle function. When file recycling is on, the delete function moves deleted files to a location specific to the platform: Windows® — Recycle bin.


1 Answers

The script is compiled by MATLAB when you call it, the compiled script is loaded into memory, and then run from memory. This is true of classes, functions, scripts, and MEX files. You can use inmem to get a list of all source files that are currently stored in memory.

If you delete the source file from within the script, it will still complete (because it is using the in-memory version), but obviously would not be available to be run again.

You can see this for yourself by sticking this in a script

%// Prove that we started
disp('About to self destruct')

%// Get the name of the current script
this = mfilename;

%// Remove the source file
delete(which(this))

%// Make sure we actually removed it
if exist(which(this))
    disp('Not deleted')
else
    disp('File is gone!')
end

%// Check that it is still in memory
if any(ismember(this, inmem))
    disp('Still in memory')
else
    disp('Not found in memory')
end

%// Demonstrate that we still execute this
disp('I am unstoppable')

If you then try to run this script again, it will not be found.

With regards to functions, scripts, etc. being stored in memory. You can always use clear to explicitly clear them out or to clear everything of a specific type from memory.

%// Clear out an explicit script
clear scriptname

%// Clear all functions & scripts
clear functions

Interestingly, even if you call clear scriptname from within the script scriptname.m, this will not prevent the script from completing.

%// Get the name of the script
this = mfilename;

%// Delete the file
delete(which(this));

%// Try to clear this script from memory
clear(this);

%// Prove that we're still running
disp('Still here')

Another interesting tidbit is that mlock is intended to prevent the currently executing function/script from being removed from memory even after it completes. If you insert this into the script (after deleting the file), the script still shows up using inmem after the script completes, however, you still can't call the script again since the source file cannot be found.

this = mfilename;
delete(which(this));
mlock
disp('Still here')

Then from the command window

%// Run the self-destructing script
scriptname

%// Check to see if it is in memory
ismember('scriptname', inmem)

%// Now try to run it again (will not work)
scriptname

Summary

So is it safe to delete a script from within itself? Yes. It seems as though you cannot prevent a currently executing script from running to completion by deleting the source file.

like image 76
Suever Avatar answered Dec 05 '22 06:12

Suever