How to bookmark a piece of code

Hi everyone,
I wonder if it's possible to bookmark pieces of code to make kinds of pre-scripts in a menu for example.
For example, can i creat a bookmark of the following script to not have to rewrite this every time i wont to plot a figure ?
figure();
plot();
title('');
xlabel('');
ylabel('');
legend;
Thanks

 Accepted Answer

Rik
Rik on 5 Dec 2020
Edited: Rik on 5 Dec 2020
I believe it is possible to add buttons that run code. You can use the clipboard function to copy this as text to the clipboard.
Personally I tend to use functions for repeating tasks (see below for an example). In this case you could write a function that allows you to input data to be passed to plot as input arguments. If you want to I could write an example.
function fig=blank_figure(varargin)
%Create a blank figure that has no fancy controls or menus.
%
% Inputs are piped to figure(), so all the normal syntaxes are supported.
%
% Creating a figure without bells and whistles is especially useful if you
% want a robust way to generate an image without the possibility of user
% interaction leading to unexpected behavior.
% Another use case is the creation of a GUI, where it is better to manually
% add back any extra controls that the required releases support.
%
% This function is compatible with all Matlab releases
fig=figure(varargin{:});
% To remove the new interactions (introduced in R2018b), we need to use
% some tricks.
%
% Use eval to trick the syntax checking in ML6.5 (both the ~ and the @ trip
% up the syntax checker).
%
% Using evalc ensures that you can group functions in a single anonymous
% function that otherwise have no output (as neither set() nor
% disableDefaultInteractivity() have output arguments).
persistent defaultAxesCreateFcn
if isempty(defaultAxesCreateFcn)
defaultAxesCreateFcn=eval(['@(ax,~){',...
'evalc(''set(ax.Toolbar,''''Visible'''',''''off'''')''),',...
'evalc(''disableDefaultInteractivity(ax)'')}']);
end
%We only need to modify the creator function on >=R2018b
try %#ok if verLessThan is missing verLessThan would have returned true
if ~verLessThan('matlab','9.5')
set(fig,'defaultAxesCreateFcn', ...
defaultAxesCreateFcn);
end
end
%remmove other controls to ensure a blank figure
set(fig,'Menu','none','Toolbar','none');
end

2 Comments

CELESTIN GALL
CELESTIN GALL on 5 Dec 2020
Edited: CELESTIN GALL on 5 Dec 2020
Thx ! If it's does't take you too much time i would like to see an example.
This weekend I'm in mobile, so if you need it fast, I would suggest giving it a shot yourself (I would actually suggest that either way).
To be clear, I meant writing an example of your plotter, not how to add a button anywhere.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Dec 2020
To set bookmarks in an editor, you can type control-F2. Then you can type F2 to move cyclically through the bookmarks. At this time there is no bookmark panel to let you see bookmarked lines and go there directly.
To reuse code snippets, I do it two ways. If they are just partial snippets, I put them all into a file called "common_code.m". Then if I need a particular snippet of code, like say to ask the user for a filename, I just go to that m-file and copy the snippet I need.
If the snippet is a fully functioning function, I put them all into a folder called "Utilities" and make sure that folder is on my path. Then anytime I need that function, I simply call it.
Or if it's a function that really only applies to your one specific program, then you can put it in your program as its own function, like down at the end of all the code.

Categories

Products

Release

R2020b

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!