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
댓글 수: 0
채택된 답변
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
추가 답변 (1개)
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!