How to move function code to a new file
1 view (last 30 days)
Show older comments
I am new to Matlab.
I have a script that computes a TIF file. I wanted to add a function to scale the TIF file. I mistakenly added the function code to the top of the script. Matlab complained that the function name did not agree with the file name. I cut the function code and pasted it into a new script file. I then changed the name of the new script file to agree with the function name. I now have two mlx files in the current folder, one for the script and one for the function.
I cannot run the script. The problem is that Matlab continues to insist that the function name and the file name do not agree and function is known by the name of the script file. Removing the function code and moving it to a new file does not seem to be sufficient.
How do I get Matlab to recognize the function is in a new file, and the file name and function name now agree?
How do I get Matlab to run the script file rather than insist that it is the function file?
2 Comments
Cris LaPierre
on 5 Sep 2020
Edited: Cris LaPierre
on 5 Sep 2020
From what you've described, it sounds like you've done what you need to. Please copy and paste the complete error message. What is the name of your script? And the name of your function? Perhaps share the first few lines of code of both your files.
You could also try restarting MATLAB first.
Accepted Answer
More Answers (1)
sjhstone
on 5 Sep 2020
As your function just scales a picture, there is really no need to put it into a live function. There are 2 simpler ways of defining a function.
- You can use a .m file to store your function. In this way, you need to keep the filename the same as you function name.
- Standalone function is not the only legal form of a function. If you don't have any other script to share this function, you can also have code blocks at the end of your live script file, and you are free to name them without modifying the filename.
The mlx format is actually a zip file, which contains machine-generated metadata underneath. You can change the file extension suffix from .mlx to .zip and unzip it to have a look.
See Also
Categories
Find more on Manage Products 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!