Matlab: Saving and Running M-files

In summary, the conversation discusses an issue with running M-files in Matlab when they have certain characters in the file name, such as "#" or "-". The option to run the file is not highlighted and adding ".m" manually to the file name is a workaround. The conversation also mentions that M-file names must start with an alphabetic character and may contain alphanumeric characters or underscores. A link is provided for more information on valid M-file names.
  • #1
teknodude
157
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
  • #2
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:
  • #3
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.
 
  • #4
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
 

1. How do I save my M-file in Matlab?

To save your M-file in Matlab, click on the "File" tab in the top menu bar and then select "Save As". Choose a location and name for your file, and make sure to add the ".m" extension at the end of the file name.

2. Can I run an M-file without opening the entire Matlab program?

Yes, you can run an M-file by using the "run" command in the Matlab command window. Simply type "run filename" and press enter to execute the M-file without having to open the entire program.

3. How can I specify the order in which my M-files are run?

You can specify the order in which your M-files are run by using the "addpath" and "run" commands in the Matlab command window. First, use "addpath" to add the directory containing your M-files to the Matlab search path. Then, use "run" to execute each M-file in the desired order.

4. Is it possible to run a section of code from my M-file?

Yes, you can run a specific section of code from your M-file by using the "Run Selection" option in the Matlab editor. Simply highlight the code you want to run and then click on "Run Selection" in the editor toolbar.

5. How can I check if my M-file has any errors before running it?

You can check for errors in your M-file by using the "Check Code" option in the Matlab editor. This will scan your code for any errors and display them in the editor window. You can also use the "Lint" option to check for potential coding issues.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
391
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Replies
19
Views
1K
  • Programming and Computer Science
Replies
20
Views
526
  • Programming and Computer Science
Replies
22
Views
921
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
755
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top