Store a script as a function

조회 수: 26 (최근 30일)
Sania Gul
Sania Gul 2024년 7월 30일
이동: Walter Roberson 2024년 8월 1일
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