Code covered by the BSD License  

Highlights from
TextInject

3.0

3.0 | 2 ratings Rate this file 7 Downloads (last 30 days) File Size: 1.74 KB File ID: #29594
image thumbnail

TextInject

by Phillip

 

01 Dec 2010

A mex file to send text to any other application.

| Watch this File

File Information
Description

This is source code for a mex file that I made, which sends text to any Windows application from within Matlab. I needed to make this because I have a device that I can only communicate with by typing into HyperTerminal, but I wanted to be able to do it automagically.

To mex this file, cd to the directory and in Matlab type:
mex 'textInject.cc'

Then use the file by typing

textInject(WindowName,X,Y,TextToSend)

Example of WindowName is 'Untitled - Notepad'
X and Y are co-ordinates, relative to the window, where the edit field is that you're trying to "type" into. Example for Notepad is X=1 and Y=1. If you are having trouble finding the correct co-ordinates, try taking a screenshot and using Paint.
For TextToSend, you can put in any string but if you want e.g. newline or carriage return you should wrap the string in sprintf() to apply the desired formatting.

Hope this is useful to somebody else as it took me a couple of days to figure out how to do this :S

MATLAB release MATLAB 7.8 (R2009a)
Other requirements Only tested under Windows XP SP3. Probably works under any modern Windows.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
30 Jan 2011 matar maoz

this is a code in c.
how can i use it in matlab?

03 Feb 2011 Phillip

Hello matar.

Cd to the directory and in Matlab type:
mex 'textInject.cc'

Then use the file by typing

textInject(WindowName,X,Y,TextToSend)

I wrote this in the comments above - did you read them?

01 Mar 2011 Jan Simon

What a cruel function! You can do a lot of ugly stuff with it. But for strange jobs, it is simply marvelous.
Matar should definitely ask before he rates.

25 Mar 2011 rahman

tnx Phillip!
I want to send a command in command line of another MATLAB.how can i use this mex?
(I want to open another matlab windows and run a Mfile in that!)

27 Mar 2011 Phillip

Rahman, I'm not sure I know how to do what you ask. When I open a new Matlab window it has the same name as the other one. Perhaps if you use two different versions of Matlab!

Even then, this function only inserts the text into the command line, it does not seem to execute it even if you put a carriage return character (\r) in.

A point of note if you want to find the Matlab window at all with this function - there are TWO spaces after "MATLAB" in the window name... eg my window name is:
"MATLAB 7.8.0 (R2009a)"

04 Apr 2011 Jan Simon

You can set the name of the Matlab window: m = com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame;
m.setTitle('Matlab instance 1');
Does CHAR(10) (\n) work instead of CHAR(13) (\r)?

04 Apr 2011 Phillip

Hi Jan,

That's a neat trick to rename the Matlab window, thanks!

Neither \n nor \r work unfortunately. However I know that the Matlab command line *can* recognize a newline because when you copy-paste code you can pick up the invisible newline and it will execute straight away when you paste it to the command line. But this internal Matlab character must not match the ANSI C standard (maybe to protect against malicious scripting attacks?).

10 Jul 2011 Poul-Erik Hansen

Great Program
I also have a device that I can only communicate with by establising a telenet connection within a windows command window or HyperTerminal. Could you give some hints about how you make your interactive script.

14 Jul 2011 Phillip

Sure. The device I communicate with is a camera. Here is an example of the code I use to change the gain of the camera:

textInject('Pulnix - HyperTerminal',100,100,sprintf(':GMFF\r'))

The : indicates the beginning of the signal and the \r indicates the end. I understand these are pretty common symbols with this type of device communication, so might apply to your device too.

The rest is device-specific for this (Pulnix) camera, indicating that we want to change Gain, set it Manually, and to a value of 255 (FF in hex format).

Hope this helps.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
windows Phillip 02 Dec 2010 13:31:21
application Phillip 02 Dec 2010 13:31:21
text Phillip 02 Dec 2010 13:31:21
mex Phillip 02 Dec 2010 13:31:21
send Phillip 02 Dec 2010 13:31:21
mex Benedetto 07 Oct 2011 13:50:53

Contact us at files@mathworks.com