How to move function code to a new file

4 views (last 30 days)
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
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.
David Clark
David Clark on 5 Sep 2020
Thanks for your reply.
I don't get an error message since I am not able to run the script. Run is grayed out.
There are some orange bars on the side of the script. If I hover over the top one I get this message:
"Line 1. The function name 'scalex' is known to MATLAB is by its file name: 'esimage'."
The function scalex is now in the file scalex.mlx.
The file esimage is the file that contains the script.
===========
I shutdown and restarted Matlab. It seems that everything is working! I don't know why that was necessary, but it worked.

Sign in to comment.

Accepted Answer

David Clark
David Clark on 5 Sep 2020
As noted in the comment above, based on Cris LaPierre's suggestion, restarting Matlab worked.
That does not seem to be a good explanation for what happened, but at least it worked.

More Answers (1)

sjhstone
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.
  1. 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.
  2. 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.
  1 Comment
David Clark
David Clark on 5 Sep 2020
Thanks for your reply.
I do plan to use this function with other scripts. Also part of my objective is to learn more about how to manage functions in Matlab.

Sign in to comment.

Categories

Find more on Manage Products in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!