Is there a way to write protect an m file.
55 views (last 30 days)
Show older comments
Hi ,
I have a project with multiple m files. And I am told that I need to write protect it, so that no one can change it. Is there any way to write protect an m file. Thank you.
2 Comments
Stephen23
on 6 Nov 2024 at 10:40
"Is there any way to write protect an m file"
An Mfile is just a text file, so you can use whatever tools your OS has for preventing text files from changing.
Or use P code: https://www.mathworks.com/help/matlab/ref/pcode.html
Bruno Luong
on 6 Nov 2024 at 11:12
Edited: Bruno Luong
on 6 Nov 2024 at 11:16
Note that even if the mfile is written protected, someone can copy it elsewhere, modify it and easily make MATLAB to use a modified version by addpath command. Those so called protections are then very weak.
pcode is a little bit harder to crack. You probably have to pcode not only the sensitive mfiles but all the caller function as well.
Answers (2)
Bruno Luong
on 6 Nov 2024 at 10:38
You could transform mfile to pfile than depioy it. See pcode command
0 Comments
Shashi Kiran
on 6 Nov 2024 at 10:39
Here are some methods to protect your files from being edited:
1. Protecting Code with P-code Files:
- MATLAB allows you to secure your code using P-code files, which prevents users from viewing or editing the source code.
- To do this, navigate to the directory containing your .m files and add them to the MATLAB path.
- Then, use the "pcode" function to create a P-code file:
pcode file1.m file2.m
Refer this for more information about P-code files
2. File System Permissions:
- You can change the file system permissions using the "fileattrib" command in MATLAB to make the files read-only.
- By using '-w', you remove the write permission, effectively making the file read-only.
fileattrib('file1.m', '-w')
Refer this for more information about "fileattrib"
- https://www.mathworks.com/help/matlab/ref/fileattrib.html
- https://www.mathworks.com/help/matlab/matlab_prog/protect-your-source-code.html
Hope this helps.
0 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!