Is there a command to copy output of fprintf (sent to the matlab terminal) to windows clipboard?

1 回表示 (過去 30 日間)
I output something to the matlab command terminal using fprintf llike this
>> fprintf('1\t2\t3\n')
1 2 3
I want to automatically copy the output "1 2 3" to the clipboard (same as manually dragging the mouse over it to select the text and pressing ctrl+C. Is there a matlab command to do this? Or is there a way to directly fprintf to the clipboard?I need it to preserve the "tab" inserted by \t.

採用された回答

Steven Lord
Steven Lord 2024 年 8 月 21 日
Use sprintf instead of fprintf and pass the output from sprintf into the clipboard function as the second input.
x1 = sprintf('%d squared is %d\n', [1:3; 1 4 9])
x1 =
'1 squared is 1 2 squared is 4 3 squared is 9 '
clipboard('copy', x1);
x2 = clipboard('paste')
x2 =
'1 squared is 1 2 squared is 4 3 squared is 9 '

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 8 月 21 日
If it is not convenient to change the fprintf() to sprintf(), then there are two ways to proceed:
  • enclose the relevant function call in evalc() and assign the result to a character vector. The resulting character vector will contain all of the text generated during that function call. Then fish through the text to extract the portion you want and clipboard() it . This approach has the disadvantage that the captured text will not be displayed to the screen (not unless you specifically display it
  • Or; use diary() to turn on logging to a file, run the command, then turn diary off. Read the text from the file, and fish through the text to extract the portion you want and clipboard() it. The generated text will be displayed to the screen.

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by