Have Matlab type a string as if from keyboard - matlab

I was wondering if anyone knew if it is possible to have MATLAB type a string for you, as if the user had typed it on the keyboard. I believe it can be done using a shell script or an applescript, but I was wondering if Matlab had a native implementation.
I have tried searching around for it, but have not had any luck. It is not super necessary, but I am just super lazy and want to write a script that will automatically enter information into an application after MATLAB has calculated stuff.
If you know of another simple way of doing this, let me know as well. Thanks a lot!
EDIT:
Adding some code that I used in response to an answer below, using the Java Robot Class
function robotType(text)
import java.awt.Robot;
import java.awt.event.*;
SimKey=Robot;
for i = 1:length(text)
if strcmp(text(i),upper(text(i))) == 0 || all(ismember(text(i),'0123456789')) == 1
eval(['SimKey.keyPress(KeyEvent.VK_',upper(text(i)),')'])
eval(['SimKey.keyRelease(KeyEvent.VK_',upper(text(i)),')'])
else
SimKey.keyPress(KeyEvent.VK_SHIFT);
eval(['SimKey.keyPress(KeyEvent.VK_',upper(text(i)),')'])
eval(['SimKey.keyRelease(KeyEvent.VK_',upper(text(i)),')'])
SimKey.keyRelease(KeyEvent.VK_SHIFT);
end
end
end
Warning, code may not be the best, but hey it was written in like 5 minutes.

I ended up writing an applescript to do everything that I needed Matlab to do. Unfortunately this will not help the Windows people, but myself and the other people using the script are mac users, so it works for us.
I have however, edited my question above to include code that I used initially in Matlab to auto type things. Simply run command as robotType('SomeString') and it will type that string.
I do not believe it will hand spaces or random characters that well, or at all, but it is good enough for abc123. Sorry for no final solution on this.

Related

Matlab rfplot not working

I'm trying to plot the S-Parameters in a file .s4p using the RFtoolbox but it seams that the function that I need rfplot doesn't work.
I searched which rfplot to see if the function exsits, and it is, but it's empty. I opened and inside I have this line:
narginchk(1,Inf)
error(message('MATLAB:UndefinedFunctionTextInputArgumentsType','rfplot',class(varargin{1})))
I tried also with other versions of Matlab (I have R2017a) where I work, but everyone have this kind of function.
Do you know why? Al the other functions of RFtoolbox work perfectly. How can I solve it if this problem is not related to my version?
The function exist, and the code exist too, but its hidden. Its a MATLAB proprietary code and they do not want you to know the real code behind it, so you can not see it.

Matlab converting library to model

I'm working on a script to convert a Simulink library to a plain model, meaning it can be simulated, it does not auto-lock etc.
Is there a way to do this with code aside from basically copy-pasting every single block into a new model? And if it isn't, what is the most efficient way to do the "copy-paste".
I was not able to find any clues as how to approach this problem here, or on Google, or on the official documentation or on the MathWorks forum so I'm at a loss on how to proceed.
Thank you in advance!
I don't think it's possible to convert a library to a model, but you can programmatically add library blocks to models like so:
sys = 'testModel';
new_system(sys);
open_system(sys);
add_block('Simulink/Sources/Sine Wave', [sys, '/MySineWave']);
save_system(sys);
close_system(sys);
sim(sys);
You could even use the find_system command to list all the blocks in a library and then loop through them all and create a new model for each using the above code.

Ipython Notebook: programmatically read and execute cells

In the ipython notebook, I would like to programmatically read and execute code cells from within a code cell itself.
Something like
if condition:
# run input cell no. 3
I found a solution here, the function execute_notebook reads an ipynb file cell by cell and executes code cells using get_ipython().run_cell().
Is there a way to do the same, i.e. without reading the cells from the an external ipynb file first? Is there a way to write macros, to reference and access code cells from within an ipython notebook?
I'm not sure if I understand your question correctly. Would you like to be able to manipulate the exact same notebook that you are currently working in? While this may be possible, it honestly sounds like a recipe for disaster because it will most likely leave your currently running notebook in an undefined state (or at the very least it would make it hard to follow what's going on).
A cleaner solution may be to programmatically generate a separate notebook in which you include precisely the code cells you need for your use case, and then execute it. (Alternatively, you can have a "template" .ipynb file with dummy code cells which you then programmatically replace with the actual code that you want to run).
I needed to do a similar thing recently and have written up a detailed example which shows how to do this. It programmatically generates a new notebook, adds a few markdown and code cells (which in this case produce a sine wave plot where the frequency is injected on-the-fly), then executes this notebook and saves the result for further postprocessing.
I hope this helps. Let me know if I completely misunderstood what you are trying to do.
hi the exact answer to your question is :
import io
from IPython.nbformat import current
with io.open("Name_of_your_notebook.ipynb") as f:
nb = current.read(f, 'json')
ip = get_ipython()
for cell in nb.worksheets[0].cells:
if cell.cell_type != 'code':
continue
if cell.prompt_number==4186:
ip.run_cell(cell.input)
but keep in mid please, the cell will be run under this code, not like as function in py does
I think the best way in this case would be to write a procedure and run it as much as you want. But as a brain workout, I would suggest two options.
To execute the code from the cell when some condition is met (suppose we want to reuse the third one as in your example), you can use exec
if condition:
exec(In[3])
More reliable solution may be with marked cells and a magic command rerun:
if condition:
%rerun -g 'unique mark'
But again, I'm pretty sure the best way is to write a procedure and reuse it.

Matlab coder and command-line workflow

I would like to use the Matlab coder to create a C/C++ dynamic library. I have a bunch of m-files (functions), which all take a scalar struct with the same fields and some other simple parameters (scalar double etc.). As there are many m- files, I would like to use the ‘command-line workflow’ to use the ‘scalar struct argument definition’ for each m- file/function (basically use ‘programmatic’ code generation instructions).
Is anyone aware of a ‘matlab coder command-line workflow’ example with more than one m-file, which uses at least one struct definition?
I also tried to ‘export project setting’, in the believe that this will generate some text file that contains the ‘programmatic’ code generation instructions. Unfortunately, I cannot find this file after the export. It is not in the project related folder. Any ideas where it would be? Thanks.
I may delete this question actually. Made some progress in the meantime. See here:
SO Question
I leave this 'answer' for now, in case someone stumbles upon this.

Perl: Determining where a function is defined [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In Perl, how can I check from which module a given function was imported?
Hi guys!
I am using my weekend time to comb through our web app to get a better understanding of it. It uses numerous modules whose functions get pulled into it and my question is this.. how can I determine the module where a function originates?
Reason I ask is because I am using print STDERR lines sprinkled here and there to understand the way data moves around (it has demystified things greatly).. here's an example....
($file_data,$count) = lookup_files($customer,$site,'','0',$d);
What I'm not sure of is where lookup_files() is originating from. What I'd like to see is this....
($file_data,$count) = lookup_files($customer,$site,'','0',$d);
print STDERR "lookup_files originates here " . <CODE TO SHOW ME WHERE lookup_files IS DEFINED>;
Any advice on where to begin would be greatly appreciated. The webapp uses tons of use modules and instead of selectively importing only what's needed, each use seems to bring all functions in.
I know my terminology might be incorrect when referring to "method", "parent" and so on in regards to Perl. If anyone would like to correct me on it that would be appreciated too. I am at best a beginner with this stuff. Janie
Take a look at the core module Devel::Peek
http://perldoc.perl.org/Devel/Peek.html#A-reference-to-a-subroutine
The output of that module's functions will tell you where a subroutine comes from
One fairly easy way is to load Carp::Always. If it’s a web app, you’ll need to put it in the code. For a command line you can just perl -MCarp::Always ...