2

iam looking for a way to get the output of an octave statement to the windows clipboard.

iam not searching a way to just manually copy/paste text from the cmd window (i know how this would work). iam also not looking for getting the whole output of a complete octave session which could be gotten by launching octave with a script to execute and piping all output to some clip.exe.
i want to capture the output from some single statement that will be executed from octave promt or some function or script.

Would be great if someone has some advice.

Edit:
from a comment i learned about the clipboard command of matlab that is unfortunally not implemented yet in octave.
maybe any other ideas involving fancy system() calls?

vlad_tepesch
  • 6,330
  • 1
  • 29
  • 67

2 Answers2

3

Well, apparently it's not too difficult to implement something fairly similar to Matlab - after a few minutes of fiddling around, behold my new clipboard.m:

function clipboard(data)
if ~ischar(data)
    data = mat2str(data);
end
data = regexprep(data, '\\','\\\\');
data = regexprep(data, '%','%%');
f = tempname;
h = fopen(f, 'w');
fprintf(h, data);
fclose(h);
system(['clip.exe < ' f]);
delete(f);
end
Notlikethat
  • 18,119
  • 2
  • 34
  • 67
  • Two remarks: 1. for linux, install xclip and use this `system()` call: `system(['cat ' f ' | xclip -selection clipboard']);` 2. you might want to use `num2str()` instead of `mat2str()` to get rid of the surrounding square brackets (stack overflow reviewers think it's more appropriate to tease apart the code, I wanted to add them directly as comments in the code snippet above from @Notlikethat; sorry for that little mess, in addition because comments don't allow for nice formatting :/ ) – kartoffelsalat Mar 09 '18 at 15:15
1

You could always call something like xclip through a system command. For examples of xclip usage, see here

The following Matlab command works for putting multiline stuff into the clipboard on Mac. Presumably you would substitute pbcopy with xclip and it would work on linux.

>> system(['echo "line1' 10 'line2' 10 'line3" | pbcopy'])
Community
  • 1
  • 1
Patrick
  • 1,543
  • 15
  • 29
  • yes but how to use it? `system("echo 234.4234 | clip.exe")` works well of course, but must things are multi line strings. – vlad_tepesch Jan 30 '14 at 11:44
  • hmm. on windows and octave `system(['echo "line1' 10 'line2' 10 'line3" | pbcopy'])` only outputs `"line1` each new line is interpreted as own command so your statements splits into the commands `echo "line` , `line2` and `line3 | pbcopy` edit: iam wrong it seems that only the first line will be evaluated. the other lines seems to be ignored at all – vlad_tepesch Jan 30 '14 at 12:29