Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running "preamble" code in MATLAB

Tags:

matlab

Is there any way to make MATLAB run a certain chunk of code every time you try to run a script? For instance, I would like MATLAB to run

sprintf('Here we go...')

as soon as I hit the Run button and then move on to execute my script, so if my script were

i = 1;
i = i * i;
display(i)

I would get

ans =

Here we go...


i =

      1

P.S. I would appreciate it if the people with higher reputation please corrected the title of my question for it to better reflect the content.

like image 284
Sia Avatar asked Sep 27 '13 02:09

Sia


People also ask

What is preamble Matlab?

A preamble is a set of symbols or bits used in packet-based communication systems to indicate the start of a packet. The preamble detector object finds the location corresponding to the end of the preamble. To detect a preamble in an input data sequence: Create a comm.

What is preamble detection?

The Preamble Detector block detects the end of preambles in data packets. A preamble is a set of symbols or bits used in packet-based communications systems to indicate the start of a packet. Packets consist of preamble data and user data.


2 Answers

as soon as I hit the Run button

I am assuming you are talking about the run button in the editor. In R2012a there was a feature called "Run Configuration". A run configuration was linked to a specific script and included code to be executed prior to the script being run. There does not appear to be a global setting to be used on all function. This feature appears to have been silently removed in R2012b.

In R2013b you can chose to run a different script. Presumably you could hack the editor to get the current file and use the custom run script to run your preamble and then the current editor file. This seems like a lot of work for not much return ...

You could create a file called myrun.m

desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
title = jEditor.getTitle;
currentFilename = char(title.replaceFirst('Editor - ',''));
fprintf('Here we go...');
run(currentFilename);

and this in the editor under run Run: type code to run type myrun. One you do this once it will remember your preferences and you can then run you code via myrun with F5. It will remember your preferences across restarts.

like image 82
StrongBad Avatar answered Oct 21 '22 01:10

StrongBad


The way to do this would be to have a preamble.m and doThis.m. In preamble.m you'd have this:

sprintf('Here we go...')

and then in doThis.m, you'd have

preamble
i = 1;
i = i * i;
display(i)

The only trick to making this work is to have them both on the path, or in the same directory.

like image 43
Andy Clifton Avatar answered Oct 21 '22 01:10

Andy Clifton