File Exchange

image thumbnail

TextInject

version 1.0.0.0 (1.74 KB) by Phillip
A mex file to send text to any other application.

6 Downloads

Updated 01 Dec 2010

View License

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

Cite As

Phillip (2020). TextInject (https://www.mathworks.com/matlabcentral/fileexchange/29594-textinject), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (15)

oh, sorry. My mistake, no bug at all. Thanks a lot.

very cool, but there is a bug.

Phillip

Hi Amirhosein,

Yes! Here is one I prepared earlier :)

http://www.mathworks.com/matlabcentral/fileexchange/40001-keyinject

hello i want to send keyboard key to a software like press a key from keyboard in a software not a string can you solve this?

hello i want to send keyboard key to a software like press a key from keyboard in a software not a string can you solve this?

Very nice!

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.

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.

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?).

Jan

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)?

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)"

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!)

Jan

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.

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?

matar maoz

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

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux