Store a script as a function

26 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2024-7-30
移动:Walter Roberson 2024-8-1
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 个评论
Sania Gul
Sania Gul 2024-7-30
移动:Walter Roberson 2024-8-1
tnk u sooooooooo mch. Its now working.
Sania Gul
Sania Gul 2024-8-1
移动:Walter Roberson 2024-8-1
I want to accept the answer of Stephen23 but there is no Accept the answer button neither any thumb vote.

请先登录,再进行评论。

更多回答(1 个)

arushi
arushi 2024-7-30
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.
  1 个评论
Sania Gul
Sania Gul 2024-7-30
编辑:Sania Gul 2024-7-30
Thanks arushi, but the problem persists even after trying the tips you have adviced me. Now I try to explain the problem with more pics. I have written the main program as given in Fig0 below.
Then I opened another folder containing the function randomstart.mlx as shown in the figure below (Left pane of matlab showing current folder)
The original contents of randomstart.mlx are as shown in the next Figure as
Then I modify it as shown below
Then when I tried to save it as .m file as recommended by you, the following message appeared.
I then click on the "Select Another File" button and saved it as .m file as shown in the next pic.
The file got stored as an ordinary file as shown in the left pane "Current Folder" of the very first pic above and when I executed the program trainDDPGSE.m, I got the error as
Now I hope u can help me in a better way. And thanks for your ealier response.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by