Store a script as a function
26 views (last 30 days)
Show older comments
I made some changes in an already existing function file "randomstart" downloaded from Mathworks reinforcement learning on ramp course. When I made few changes in that file and tried to save it, I got the message as shown in the pic attached
when I clicked on the overwrite button, the script file no longer remains a function file. It simply changes to an ordinary .mlx file which cannot be called from the main function of trainDDPG.mlx. Similar is the case with 'Select Another File..." option. Can anyone help me what to do? Thanks
0 Comments
Accepted Answer
Stephen23
on 30 Jul 2024
Moved: Walter Roberson
on 1 Aug 2024
If you want to call RANDOMSTART from e.g. another function or from the command line then it must be a function. What you have is not a function because you added two lines of code outside of the function definition (the two lines both start with DISP(..). Those lines mean that you have created a script, not a function that you can call:
Solution: delete all lines of code outside of the function definition. Then you can call the function.
2 Comments
More Answers (1)
arushi
on 30 Jul 2024
Hi Sania,
It sounds like you're encountering an issue where saving changes to a `.m` function file in MATLAB is causing it to be converted to a `.mlx` live script file. Here are some steps you can take to resolve this issue:
1. Ensure Correct File Extension
Make sure you are saving the file with the correct `.m` extension. When you click "Save As" or "Save," ensure the file name ends with `.m`.
2. Use MATLAB Editor
Open the file using the MATLAB Editor to ensure it is recognized as a function file. You can do this by navigating to the file in MATLAB's Current Folder window and double-clicking it, or by using the `edit` command:
edit randomstart.m
3. Save As Function File
If the file is open in the Live Editor (which is used for `.mlx` files), you need to switch to the standard Editor.
- Pasting the content into this new script.
- Saving the new script with the `.m` extension.
4. Check File Permissions
Ensure you have the necessary permissions to overwrite the file. If you don't have write permissions, you might need to change the file permissions or save the file in a different directory where you have write access.
Hope this helps.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!