Making a *.m file "read only."

31 views (last 30 days)
Truman
Truman on 17 Feb 2015
Edited: per isakson on 17 Feb 2015
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.

Accepted Answer

Sean de Wolski
Sean de Wolski on 17 Feb 2015
Edited: Sean de Wolski on 17 Feb 2015
MATLAB doesn't have the ability to do this. However, you could use a source control system like SVN or GIT to monitor them. Then you'd be able to see if any changes had been made since the last commit and what those changes are (in case you do decide to add a comment or something!). In R2014b, you can do this from the current folder browser.

More Answers (1)

per isakson
per isakson on 17 Feb 2015
Edited: per isakson on 17 Feb 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

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!