write octave/matlab output to clipboard - windows

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?

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

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'])

Related

How can I execute Windows commands from Perl without leaving command windows open?

I have a Perl script on Win10 that uses the system() command to run a couple of different command line processes, including:
Start Windows Media Player with a specified .mp3 file.
my $cmd = "start call wmplayer.exe myRadoShow.mp3";
system($cmd);
Start another Perl program that does something else at the same time (specifically reads and broadcasts a set of timecodes and titles).
my $cmd2 = "secondScript.pl some_params";
system($cmd2);
All of this works correctly; the minor problem is that #1 above starts up a new command line window each time it executes the system($cmd) command. I have to later go back and close those windows.
If I don't use "start call" the Perl script doesn't continue to #2.
Is there a preferred way to execute #1 that doesn't leave these windows open?
I realize this question may be more about Windows commands than Perl.
Firstly, If youre using active perl versions, there is a wperl.exe you can call your script with instead of the default perl.exe. Not to sure if this will hide sub processes created by your script but you can give it a go.
If that doesnt work, then maybe you can use Win32::GUI:
use Win32::GUI;
my $hw = Win32::GUI::GetPerlWindow();
Win32::GUI::Hide($hw);
there are other modules that provide this functionality as well. Good luck!

Running gnuplot from Fortran program under Windows

I need to make a Fortran program(.exe) that analyses data and builds a plot.
I've decided to do this by calling gnuplot from my Fortran program along with a gnuplot settings file.
When I do this from Windows Command Line:
wgnuplot -persist input.txt
it works fine. But when I call it from Fortran program like this:
h=SYSTEM("wgnuplot -persist input.txt")
that doesn't open gnuplot.
At the same point, if I simly write:
h=SYSTEM("wgnuplot")
it successfully opens gnuplot (but, obviously doesn't draw a plot)
How can I call a gnuplot with a gnuplot settings file from the Fortran program?
I've tried to specify the whole path to the files, that doesn't help.
Also I need "-persist" key to be there, because running a command without it in CMD:
wgnuplot input.txt
does not work for me.
I've also tried all that with EXECUTE_COMMAND_LINE command, and also tried to do this with C++ code like this:
int main(){
char command[100];
strcpy( command, "wgnuplot -persist 'gp.txt'" );
// strcpy( command, "wgnuplot" ); // but this one works alright!
system(command);
return(0);
}
But I still get the same result - I can't run gnuplot with an argument file.
What am I doing wrong? Is there another way to achieve my goal?
Well, it turned out, that the problem was with the input.txt file itself.
So actually this command works perfectly on Windows:
h=SYSTEM("wgnuplot -persist input.txt")
#shellter, #agentp, #chw21 Thank you for your help!

Ignore user-input when running a unix command from within Matlab

I have a Matlab program that runs different unix commands fairly often. For this question let's assume that what I'm doing is:
unix('ls test')
It happens to me quite frequently that I accidentally press a key(like enter or the arrow keys) f.e. when I'm waking up my display from standby. In theory this shouldn't interfere with the unix command. Though unfortunately, Matlab will take this input and forward it right into the execution of the command. The above command then becomes something like this:
unix('ls te^[0Ast')
(Side note: ^[0A is the hex representation of the linefeed character)
Obviously, this will produce an error.
Does anyone have an idea how to work around this issue?
I was thinking that there might be a way to start Matlab with my script in a way that doesn't forward any user input from within the unix shell.
#!/bin/bash
matlab -nodisplay -nosplash -r "runMyScript();"
Can I somehow pipe the user-input somewhere else and isolate Matlab from any sort of input?
That is not very specific question, but let me try. I can see several options. I am assuming that matlab is text terminal application.
There is nohup(1) command. Since you use linux, chances are that there is non-posix version if it which says in it's man page: If standard input is a terminal, redirect it from /dev/null.
$ nohup matlab -nodisplay -nosplash -r "runMyScript();"
You can redirect /dev/null yourself
$ matlab -nodisplay -nosplash -r "runMyScript();" < /dev/null
But matlab can actually re-open it's stdin ignoring what you piped into it (for example ssh does that, you can't use echo password | ssh somewhere.
if you are running in graphics environment you may want to minimise the window, so that it does not receive any input. Probably not your case, you would figure out yourself :)
you may try to wake up by hitting "Ctrl", similar key or mouse
You may run matlab in screen(1) command disconnect from the screen or switch to different window. Screen is a program allowing you to create virtual terminals (similar to virtual desktops in GUI). If you haven't heard of screen, I suggest you to look at some tutorials. Googling for gnu screen tutorial seems to offer quite a few.

Starting a Background Instance of Matlab on Mac OS X

In Windows you can use the following command in Matlab to start a new instance of MATLAB which will run in the background (i.e. you can keep executing commands in your first version of MATLAB).
system('matlab &')
An analogous call in OSX,
system([matlabroot '/bin/matlab &'])
however results in the display of the splash image, then nothing. If I take out the ampersand, the new instance opens as expected. Unfortunately, this won't work for me, I really need to be able to control the first instance of MATLAB while the second is running.
Does anyone know why this discrepancy between the operating systems exists? By the way, I'm using OSX 10.7, Windows 7 64 bit, and MATLAB R2012a on Mac and R2012b on PC.
As some background, I'm trying to write a generic tester for an interactive command line interface that uses the input() function extensively.
Edit: I should have mentioned that the command
/Applications/MATLAB_R2012a.app/bin/matlab &
works as expected from the OSX terminal. In other words, a new instance of MATLAB opens and new commands can be entered into the terminal. So this problem seems to be specific to the system() function in OSX matlab.
Also, I tried adding that command to a bash script and calling the script from matlab, but had the same problem that I did with putting the command into the system() function.
Thanks
This is a long shot, but it might be happening because when you invoke the new instance of Matlab from Matlab with the system() command on Unix or OS X, the matlab_helper process forks and runs a shell process to run the new application. If you omit the ampersand, the shell blocks and waits for the program to finish, and system() waits for it, so the first Matlab locks up. And (here's the speculation part) if you add the ampersand, Matlab launches in the background, and then the forked shell exits, which then causes the new Matlab process to exit because its parent process (the shell) has exited. (Windows doesn't have the same parent/child process relationships, process launch mechanism, or shells, which would explain the different behavior.)
You could try prefixing the command with nohup, which protects processes from getting killed by SIGHUP, which might be what's happening here to your second Matlab process.
system(['nohup ' matlabroot '/bin/matlab &'])
You could also try using the OS X open command to launch a new independent instance. Something like this. You may need to fiddle with the options and path, but -n should be what gives you a new instance. It should be pointing at /Applications/MATLAB_R2012a.app; I'm assuming that's what matlabroot is returning on OS X.
system(['open -na ' matlabroot])
You could also try running it from the Java process-launching features from within Matlab instead of with system(). Runtime.exec() doesn't block like system() does, and there may be other quirks to system(), like the matlab_helper architecture. Try launching it with java.lang.Runtime from Matlab.
jrt = java.lang.Runtime.getRuntime();
newMatlabProcess = jrt.exec([matlabroot '/bin/matlab']);
You can try the other command line variants above using this mechanism too, and you may need to redirect stdout to /dev/null, since the new processes input and output are buffered in to that newMatlabProcess object.
You can use applescript to do this. I do something like this:
! osascript -e "tell application \"Terminal\" to do script \"cd `pwd`;matlab -nojvm -nosplash -r 'why'\""
This example opens a new Matlab instance, in the current directory, and runs the command "why". You can remove the "-nojvm" if you need java in your background Matlab process

Matlab system call

If I want to call a script "filtermapq.sh" inside a matlab script. How would I go about calling it and then waiting for the script to finish before resuming the rest of the matlab code? I am not the best with matlab.
Currently I am using this command:
system(['./util/filtermapq.sh ' var1 var2 var3])
However, I don't think that the matlab code waits for this to finish before continuing.
I see that you found a solution, let me give you a bit more elegant one, which will make life easier for the future.
system(sprintf('./util/filtermapq.sh %s %s %s', var1, var2, var3))
This could also pass in numbers, for instance, or other cool stuff. Also, you could do this to help with debugging such issues.
command=sprintf('./util/filtermapq.sh %s %s %s',var1, var2, var3);
fprintf('%s\n',command);
system(command);
That will log out to the screen the exact command that you are attempting to run. If your system command doesn't work, copy/paste it into a command line window, see if it works there. If it doesn't figure out how to massage the text to make it work, and fix your code appropriately.
I figured out the problem in my line of code. The problem was that matlab was not interpreting spaces between the variables I was inputting and was instead stringing them all together in one large string. Where my script takes 3 variables. I hope this helps anyone in the future, the correct code is as below:
system(['./util/filtermapq.sh ' var1 ' ' var2 ' ' var3])
One way is to have the script create a temporary file when it starts and then remove that file at the end. Within matlab, you would simply run a do... while loop checking for that file. As soon as the file is removed, you know the process has completed.
A nice introduction: http://blogs.mathworks.com/desktop/2010/05/17/calling-shell-commands-from-matlab/
Edit: Quote from the link.
"By default [system] will pause the MATLAB execution until the system command exits."

Resources