When calling a function involving multiple other functions where some changes are made, new changes don't take effect even when saving and running the newly changed function. What can one do?

댓글 수: 12

Sibghat
Sibghat 2024년 3월 3일
Have you tried restarting MATLAB or the 'clear' function_name or clear functions method and running the code again?
Sometimes MATLAB might cache function definitions, and restarting MATLAB can refresh this cache, ensuring that the latest changes are recognized. You can also try clearing MATLAB's function cache using the clear functions command.
Stephen23
Stephen23 2024년 3월 3일
편집: Stephen23 2024년 3월 3일
Also if you are using any function handles: the function remains the same until the handle goes out of scope (much like any other variable).
feynman feynman
feynman feynman 2024년 3월 3일
@Sibghat@Stephen23 Thank you! Say I have a main.m that calls function1.m, which calls function2.m. I make frequent changes in function2.m so much so that I don't bother close function2.m but just save or run it after any changes. Then where should I put 'clear' function_name or clear functions commands?
Saving the file should be sufficient that it's updated, but you might want to double-check to make sure that the copy that's being saved is the same copy that MATLAB is using. Maybe that's a shot in the dark, but it's one possible explanation.
which function2
Steven Lord
Steven Lord 2024년 3월 3일
Another potential reason the changes may not be recognized is if you're storing your files under matlabroot. Leave that for MathWorks-provided files as MATLAB caches that directory.
feynman feynman
feynman feynman 2024년 3월 4일
@Steven Lord Thanks for the tip I didn't store files under matlabroot
feynman feynman
feynman feynman 2024년 3월 4일
@DGM Thank you. which function2 shows everything normal.
I'm calling different functions in different folders via
switch solver
case 'solver1'
addpath 'D:\matlab\project1\';
solver1
case 'solver2'
addpath 'D:\matlab\project2\';
solver2
Could this frequently using addpath cause any problems?
Sibghat
Sibghat 2024년 3월 4일
You can place the clear commands in your main.m script at the beginning or end of your main.m script before calling any functions. This way, every time you run the main script, it will clear any cached versions of the functions being called (function1.m and function2.m).
Sibghat
Sibghat 2024년 3월 4일
@feynman feynman I think frequently adding paths can clutter the MATLAB search path with unnecessary directories. Although there are only two "addpath" commands in the provided code, if this code is executed repeatedly, it can result in the same directories being added to the MATLAB search path multiple times. Moreover,,if the directories being added contain functions or scripts with the same names, MATLAB may prioritize one over the other based on the order in which they're added to the path, leading to ambiguity and potential errors.
One possible solution can be to remove the paths when no longer needed. After executing the code in a specific directory, consider removing it from the MATLAB path using the 'rmpath' function to keep the path clean and avoid conflicts.
rmpath(folderName)
feynman feynman
feynman feynman 2024년 3월 4일
Thank you so much these tips seem to work!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Search Path에 대해 자세히 알아보기

태그

질문:

2024년 3월 3일

댓글:

2024년 3월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by