Store a script as a function

30 visualizaciones (últimos 30 días)
Sania Gul
Sania Gul el 30 de Jul. de 2024
Movida: Walter Roberson el 1 de Ag. de 2024
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

Respuesta aceptada

Stephen23
Stephen23 el 30 de Jul. de 2024
Movida: Walter Roberson el 1 de Ag. de 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 comentarios
Sania Gul
Sania Gul el 30 de Jul. de 2024
Movida: Walter Roberson el 1 de Ag. de 2024
tnk u sooooooooo mch. Its now working.
Sania Gul
Sania Gul el 1 de Ag. de 2024
Movida: Walter Roberson el 1 de Ag. de 2024
I want to accept the answer of Stephen23 but there is no Accept the answer button neither any thumb vote.

Iniciar sesión para comentar.

Más respuestas (1)

arushi
arushi el 30 de Jul. de 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.
  1 comentario
Sania Gul
Sania Gul el 30 de Jul. de 2024
Editada: Sania Gul el 30 de Jul. de 2024
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.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by