フィルターのクリア

Displaying command line output in App live

32 ビュー (過去 30 日間)
Jeffrey Decker
Jeffrey Decker 2023 年 11 月 1 日
回答済み: Walter Roberson 2023 年 11 月 2 日
A while ago I saw that it is possible to display the command line output from code being executed inside an App. Essentially, I have an app with multiple buttons. Each button corresponds to a script and by clicking the button, you will run that particular script. I want there to be a component within the app that displays the command line execution of the selected script. Is this possible. Similar to the plot component but instead of plotting data, something is displaying command line output.
I have lost the previous post describing the solution to this. Could someone please point me in the right direction?

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 11 月 2 日
You can use evalc to evaluate code and get back character representation of the output after complete execution of the code. You can then display the text in a uitextarea
If you need to display "as it executes", then the (more or less) supported method is to use diary to log to a file, and meanwhile to use fopen and commands like fgetl to read lines from the file as they are written. In theory, MATLAB does line buffering of output to the diary. (In practice... sometimes it doesn't, and output might not be available for a while.)
If you are reading from the diary as it is created, you may need to make use of fseek and ftell to avoid getting locked up. fgetl() and fread() from a file do not have any timeout, so if a partial line has been output to the command window then a partial line might be in the input buffer, and if you fgetl() that function might wait indefinitely for a newline to show up in the buffer.
So you might need strategies such as ftell() to record your current position. fseek() to end of file: this will seek to the current end-of-file. ftell() to find out where end of file is. Subtract the reading of the current position to determine how many unread bytes of input is in the file. Now guess that if the amount of data is more than the longest line you expect, then figure that you must have at least one line of input. But what if the amount of data available is longer than the shortest line you expect (which might be single byte if it is just an empty line with newline), but less than the longest line you expect? Hmmm -- in that case instead of using fgetl() you could fread() specifying that many bytes to read... and then parse it to figure out whether any new lines are present. If so then pull out the complete lines from the buffer and finally fseek() to position yourself just after the last newline that you read...

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by