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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With