Since Matlab R2008a, I could use the more convenient javaMethodEDT function, but I wanted my code to work on all prior Matlab 7 versions, where javaMethodEDT was not yet available. In this code snippet, note that we space-pad prompt string that are shorter than 3 characters: this is done to prevent an internal-Matlab mixup when displaying additional text – Matlab “knows” the Command-Window’s text position and it gets mixed up if it turns out to be shorter than expected.Īlso note that I use the semi-documented awtinvoke function to replace the default prompt (and an automatically-appended space) on the Event-Dispatch Thread (more on this in a future article). % The Command-Window text should be modified on the EDTĪwtinvoke(jTextArea,'replaceRange( II)'. Try jTextArea = jTextArea.java catch, end %#ok % Note: catch a possible trailing newline % Does the displayed text end with the default prompt? newPrompt (end- 2: end ), newLen- 3, newLen ) If ~isempty (pos ) % Short prompts need to be space-paddedĮnd % The Command-Window text should be modified on the EDTĪwtinvoke (jTextArea, 'replaceRange( II)'. Pos = strfind (cwText ( max ( 1,end- 3 ): end ), '> ' ) % Does the displayed text end with the default prompt? % Note: catch a possible trailing newline try jTextArea = jTextArea. If nargin & ~isempty(newPrompt) & ~strcmp(newPrompt,'> that we have the Command-Window object callback set, we need to set the logic of prompt replacement – this is done in the internal Matlab function setPromptFcn. JTextArea = cmdWin.getComponent(0).getViewport.getComponent(0) JDesktop = .MLDesktop.getInstance ĬmdWin = jDesktop.getClient('Command Window') getFocusOwner Įnd % Instrument the text area's callback if nargin & ~isempty (newPrompt ) & ~ strcmp (newPrompt, '> ' ) set (jTextArea, 'CaretUpdateCallback', } ) Įlse set (jTextArea, 'CaretUpdateCallback', ) % Get the reference handle to the Command Window text area This callback is fired whenever the desktop text is modified, which is an event we trap to replace the displayed prompt: The solution involved finding the Command-Window reference handle, and setting one of its many callbacks, in our case CaretUpdateCallback. I decided to use the callback method to set the prompt – interested readers can try the PostSet method. The first of these methods is a well-known Matlab practice, although we shall see that it uses an undocumented callback and functionality the PostSet method is entirely undocumented and not well-known and shall be described in some later article. There are two methods of handling multiple asynchronous events in Matlab: setting a callback on the object, and setting a PostSet handle.listener (or schema.listener) on the relevant object property. But this case is different since cprintf runs once synchronously (user-invoked), whereas the prompt appears asynchronously multiple times. So the prompt can (probably) not be modified in advance, but what if it could be modified after being displayed? It is true that my cprintf utility modifies the Command-Window contents in order to display formatted text in a variety of font colors. At first thought this cannot be done: The Command-Window prompts are hard-coded and to the best of my knowledge cannot be modified via properties or system preferences. A few days ago, a reader emailed me with a challenge to modify the standard matlab Command-Window prompt from “> ” to some other string, preferably a dynamic prompt with the current timestamp.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |