Executing code that's dynamically written to a file

조회 수: 7 (최근 30일)
Jim Hokanson
Jim Hokanson 2015년 6월 22일
댓글: Jim Hokanson 2015년 6월 23일
Hi all,
My goal is to write a function that executes whatever is copied to the clipboard. Importantly however, I'd like to place this code into a script, so that if an error occurs, the location of the error can be identified via 'dbstop if error'.
I've written code that does this, however Matlab doesn't seem to update it's 'compiled' version of the script until after execution. Thus, only after I run the code twice does the code actually reflect what is in the clipboard. The first time it is written and any old code is actually run. The second time through the new code is present and executed.
i.e.
1st run - write to script, execute old code from script in memory
2nd run - write to script (not really needed), execute new code that was placed in memory after exiting first run
I thought about creating a timer, but code in a timer tends to execute in the ether without easy debugging support. Besides wrapping my timer in a try/catch, is there some other way to force updating the definition of the file or to throw some event based callbacks which would update the file?
Thanks, Jim
  댓글 수: 2
Mukul Rao
Mukul Rao 2015년 6월 23일
Hi Jim, would it possible to include the code as an attachment? Further can you also specify the version of MATLAB you are working with. Based on the information provided , I would recommend using the function "rehash" right after you create your script file. This will update the list of known files and classes. Here is the documentation link for the "rehash" command for more information:
Jim Hokanson
Jim Hokanson 2015년 6월 23일
It makes calls to other functions in that repo. Thanks for the suggestion although 'clear' works very well so I'm going to use that.
I will tend to copy examples in my documentation that are commented out and then type this command in the command window. It needs a bit of work but it is nice to have errors be debuggable rather than just seeing an error in the command window.

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

채택된 답변

James Tursa
James Tursa 2015년 6월 23일
What happens if you "clear" the script from memory before invoking it?
  댓글 수: 1
Jim Hokanson
Jim Hokanson 2015년 6월 23일
Doh! Obviously. I tend to forget that clear isn't specific to variables. That works perfectly. Thanks.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by