MATLAB Matlab: Saving and Running M-files

  • Thread starter Thread starter teknodude
  • Start date Start date
  • Tags Tags
    Matlab Running
AI Thread Summary
Matlab has specific restrictions on m-file naming conventions that can affect file execution. Users have reported issues when saving m-files with special characters, such as hashes (#), periods, or hyphens in the filename. For example, saving a file as "HW#4_3.40" prevents it from being recognized as a valid m-file, while simpler names like "HW4" work without issue. The problem arises because Matlab requires m-file names to start with an alphabetic character and only allows alphanumeric characters or underscores. Users have found that removing special characters resolves the issue, but even then, periods in filenames can lead to complications. A workaround involves manually adding ".m" to the filename after saving, although this is not an ideal solution. Overall, adhering to Matlab's naming conventions is crucial for proper file functionality.
teknodude
Messages
150
Reaction score
0
I've noticed a weird thing that Matlab does whenever I save my m-file then try to run it. If I save my M-file with a simple file name like "HW4" or "untitlited" it will run. However if i save my m-file as file name HW#4_3.40 (I just wanted to label the file with the problem # from the text I'm using), the option for run is not highlighted and it doesn't work if I do command run HW#4_3.40. I also made sure that the target directory is correct. When I check the folder I saved the m-file file name HW#4_3.40 is detected by windows as an unknown file, while other files with simple names is detected as m-file.

I searched a little to maybe find out if this is a bug or something. I'm somewhat new to MATLAB so I'm not sure if there are some restrictions to filenames for m-files. Also I am using Matlab R2006a.
 
Physics news on Phys.org
Try taking out the hashes from the filename.
M-file names must start with an alphabetic character, may contain any alphanumeric characters or underscores[...]

http://www.caspur.it/risorse/softappl/doc/matlab_help/techdoc/matlab_prog/ch8_pr15.html
 
Last edited by a moderator:
Thanks for the link. I tried taking out the #, but it didn't work. I played around with isvarname to test the file names. Guess it doesn't like periods or even hyphens. I did find a way around this by going to the folder the file is saved in and adding a ".m" to the end of the filename, but that's somewhat annoying to keep doing.
 
When you enter the file's name to be saved, the dialog box will automatically append .m, but only if there are no other periods already in the file name.

- Warren
 
Back
Top