How to move function code to a new file
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
David Clark
el 5 de Sept. de 2020
Respondida: David Clark
el 5 de Sept. de 2020
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 comentarios
Cris LaPierre
el 5 de Sept. de 2020
Editada: Cris LaPierre
el 5 de Sept. de 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.
Respuesta aceptada
Más respuestas (1)
sjhstone
el 5 de Sept. de 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.
Ver también
Categorías
Más información sobre Manage Products en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!