-1

In Octave, I have a string

outstr = 'Line1 \n Line2 \n Line3 \n'

which I like to copy to the clipboard and then insert it into an Excel-sheet by pasting from the clipboard.

The clipboard function of octave does not work at all, even for simplest strings. I don't know why. And directly storing to Excel is not an option as not suffiently flexible.

As I am already using vbscript with Octave with other tasks, I'd like to use it in this case as well. So here is what I came up with:

Vbscript (borrowed from the final answer here:

Set objShell1 = WScript.CreateObject("WScript.Shell")
objShell1.Run "cmd /c echo " & Trim(WScript.Arguments(0)) & "|clip"
Set objShell1 = Nothing

which works great for one-line strings.

Then I use this wrapper function in octave:

function copy2clipboard(outstr)

pathWrapper =  fileparts(mfilename('fullpath'));
system( [pathWrapper '\copy2clipboard.vbs "' outstr '"'] );

end

For one-line strings this all works as desired, but VBscript is not able to process the newline character \n. I couldn't manage to substitute it with the VBscript equivalents like VbCrLf.

Solutions working for Matlab are fine, but only using octave-compatible syntax.

What do you suggest?

Wolfie
  • 27,562
  • 7
  • 28
  • 55
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • 1
    *I couldn't manage to substitute it* Why not? What happened when you tried that? `\n` does not have any special meaning in VBScript. – Geert Bellekens Mar 10 '23 at 10:11
  • 1
    Does this answer your question? [Use clipboard from VBScript](https://stackoverflow.com/questions/128463/use-clipboard-from-vbscript) – Geert Bellekens Mar 10 '23 at 10:13
  • 1
    MATLAB and Octave are not the same language, and the `clipboard` function in MATLAB may be implemented differently... tag removed to avoid irrelevant answers – Wolfie Mar 10 '23 at 10:43
  • @GeertBellekens I tried e. g. the string `'Line1 vbCrLf Line2 vbCrLf Line3 vbCrLf'` in Octave, which does not what it should in the VBscript Output. I guess, within VBscript, I need something like `"Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3"`, but I don't know how I could achieve that. – Robert Seifert Mar 10 '23 at 12:41
  • @Wolfie I disagree and set the Matlab tag on purpose, I do not want to use the clipboard function and the solution I am looking for is most likely equivalent in both languages. I explicitely stated, that I am only looking for compatible solutions - and I do not want to exclude the Matlab Experts from this question. – Robert Seifert Mar 10 '23 at 12:43
  • Arguably if you want a solution using `system()` this isn't a MATLAB or Octave question, and if you want a solution specific to Octave which doesn't require a system call then it might not be compatible with MATLAB – Wolfie Mar 10 '23 at 12:45
  • @GeertBellekens Regarding the duplicate question: I don't see how this solves the problem from my previous comment. How can I transform my string containing `\n` to a form like `"Line 1" & vbCrLf & "Line 2"` which VBscript can process. – Robert Seifert Mar 10 '23 at 12:50
  • @Wolfie I can agree to that. However, I feel the Matlab tag does not hurt here. I am not necessarily limited to the system call function. Also part of the question is: what is the correct input within octave/matlab? If the final solution is a pure octave of even pure system call solution, even both tags could be removed. – Robert Seifert Mar 10 '23 at 12:55
  • 2
    @RobertSeifert Please add the additional info to the question instead of in comments. If you don't know how to transform `"line1 \n line2"` into `"line1" & vbCrlf & "line 2"` then you should actually ask **that** question. – Geert Bellekens Mar 10 '23 at 13:04
  • @GeertBellekens No, because, I don't know if thats the solution. And I start to feel a little bugged by all the critics regarding the question instead of trying to help. I am member for 10 years here, I know how to ask questions and it does not seem too bad, and it contains all information a potential answerer needs. So please just leave it be. – Robert Seifert Mar 10 '23 at 13:28
  • @RobertSeifert To substitute in VBScript use `Replace()`, something like `Replace(WScript.Arguments(0), "\n", vbCrLf)`. – user692942 Mar 11 '23 at 08:53
  • Does this answer the question? [Is there "\n" equivalent in VBscript?](https://stackoverflow.com/a/2203735/692942). – user692942 Mar 11 '23 at 08:57
  • @user692942 It did not work directly, there were too many overlapping issues. But I finally found a working solution, see answer. Thank you! – Robert Seifert Mar 13 '23 at 10:10

1 Answers1

0

I finally found a working a solution. The octave wrapper function from the question

function copy2clipboard(outstr)
   pathWrapper =  fileparts(mfilename('fullpath'));
   system( [pathWrapper '\copy2clipboard.vbs "' outstr '"'] );
end

can remain unchanged. The VBscript copy2clipboard.vbs has to be adapted as follows to use multiple echo, LFC can be chosen freely:

Set objShell1 = WScript.CreateObject("WScript.Shell")
str = Replace(WScript.Arguments(0), "LFC", "&echo ")
objShell1.Run "cmd /c (echo " & str & "off)|clip"
Set objShell1 = Nothing

Important to note: The input outstr within Octave/Matlab must not contain the acutal line feed character \n. Instead a pattern of real characters has to be defined. In my case I chose LFC, as it would never appear in my output.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • @Wolfie Because I stated in my question from the beginning, that the `clipboard` function in Octave somehow does not work. And that Matlab solutions are welcome, if compatible with octave, which - using clipboard - is not the case. Otherwise it would be trivial. Thanks anyway! Would be interesting to know if your solution would work for somebody in octave - then I would prefer it, obviously. – Robert Seifert Mar 15 '23 at 17:18