Making a *.m file "read only."
Afficher commentaires plus anciens
I have several *.m files that are finished. However, I would like to set breakpoints in the debugger and watch the flow of data through them. However, I do not want to accidentally modify any of this code. Is there some way to make these routines "read only" so I can't accidentally modify them? I know I can do it at the OS level but does Matlab have the functional ability to accomplish this.
Réponse acceptée
Plus de réponses (1)
per isakson
le 17 Fév 2015
Modifié(e) : per isakson
le 17 Fév 2015
I didn't read your question to the last sentence, but don't delete my answer.
I use this function, cmd_read_only to avoid editing files, which I open in the editor just to refresh my memory. Together with this file I have (on another computer) a shortcut that runs the file on the current file. Windows only!
function varargout = cmd_read_only( filespec, action )
% cmd_read_only sets or clears the attribute, read-only, of file
%
% Example:
% sts = cmd_read_only( 'c:\m\my_file.txt', 'on' )
% sts = cmd_read_only( 'c:\m\my_file.txt', 'off' )
% ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [+I | -I]
% [drive:][path][filename] [/S [/D] [/L]]
%
% + Sets an attribute.
% - Clears an attribute.
% R Read-only file attribute.
%
% See also: fileattrib
narginchk( 2, 2 )
str = validatestring( action, {'on','off'} );
assert( exist( filespec, 'file' ) == 2 ...
, 'Pia:cmd_read_only:cannotFindFile' ...
, 'Cannot find file, "%s"' ...
, value2short( filespec ) )
if strcmp( str, 'on' )
dos_cmd = sprintf( 'attrib +R %s', filespec );
message = 'read-only: ON';
else
dos_cmd = sprintf( 'attrib -R %s', filespec );
message = 'read-only: OFF';
end
[ sts, msg ] = system( dos_cmd );
assert( sts == 0 ...
, 'Pia:cmd_read_only:FailedSetAttribute' ...
, 'Failed to set attribute: "%"' ...
, msg )
if nargout == 0
disp( message )
varargout = {};
elseif nargout == 1
varargout = { sts };
elseif nargout == 2
varargout = { sts, msg };
else
error('Wrong number of outputs')
end
1 commentaire
Sean de Wolski
le 17 Fév 2015
Very nice!
Catégories
En savoir plus sur Desktop dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!