Share your favorite snippet or utility

조회 수: 4(최근 30일)
Matt Fig
Matt Fig 2012년 9월 20일
I am wondering what others use for those little short-cuts or niceties in MATLAB. I have in mind something you wrote or something somebody else wrote or an underused MW function.
Here are my two favorites.
This is a simple script I use. Here is the entire contents of CLC.m (yes, it is capitalized):
clear all,close all,clc
Very simple, but I use it all the time. Here is another one I use so often that I forget not every machine has it (though every machine should, IMO):
Here is an underused MW function that I occasionally employ when working on someone else's machine. The usual response is, "Wait, what did you just do?"
What are some of yours?
  댓글 수: 8
Matt Fig
Matt Fig 2012년 9월 21일
편집: Matt Fig 2012년 9월 21일
@Sean, I have only the command window and a minimized command history open to the left of that space. No docked anything. I am old-school. That is why I like whats so much, one simple command and I can do it all and I don't have to keep any more panels open all the time. Let me know "what" you think if you try it.

댓글을 달려면 로그인하십시오.


José-Luis 2012년 9월 20일
편집: Matt Fig 2012년 9월 20일
Not really a snippet, but useful.
  댓글 수: 7
Sean de Wolski
Sean de Wolski 2012년 9월 21일
keep also safe guards against deleting things when you have a typo.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2012년 9월 20일
I have another few shortcut buttons on my toolbar to quickly go to often-used folders and show the file panel. For example one button that says "Go to demos folder" and in there is this code:
I keep all my demos that I post here in Answers (that are worthy of keeping) in that folder.
  댓글 수: 4
Matt Fig
Matt Fig 2012년 9월 20일
IA, I gave a link in the OP to whats. Here is another:
The function shows all M-files, MAT-files and figure files in the directory as hyperlinks. Clicking on a M-file name opens the file for editing. Clicking on a MAT-file name loads the file. Clicking on a figure file opens the figure.

댓글을 달려면 로그인하십시오.

Daniel Shub
Daniel Shub 2012년 9월 20일
I have three versions of your CLC that I have as shortcuts. They all came from here
The biggest hammer is
!matlab &
This sometimes causes issues with the command window moving and files needing to be closed, but it really gets the job done. The second is a slightly smaller hammer that is based on the longer answer to the question. The third is the same as the second, except it doesn't screw with the path, current directory, or command window contents.
On my computer get(0, 'MonitorPosition') returns nonsense so I use
function MonitorPositions = getmonitorpositions()
error(nargchk(0, 0, nargin, 'struct'));
error(nargoutchk(0, 1, nargout, 'struct'));
Monitors = ...
MonitorPositions = zeros(length(Monitors), 4);
for iMonitors = 1:length(Monitors)
MonitorBounds = Monitors(iMonitors).getDefaultConfiguration.getBounds;
MonitorPositions(iMonitors, :) = ...
[MonitorBounds.getX, MonitorBounds.getY, ...
MonitorBounds.getWidth, MonitorBounds.getHeight];
I also like
function [UserNameString, HostNameString, IpAddressString, MacAddressString] = getcomputer()
error(nargchk(0, 0, nargin, 'struct'));
error(nargoutchk(0, 4, nargout, 'struct'));
UserNameString = char(java.lang.System.getProperty(''));
HostNameString = char(;
Eth0Obj ='eth0');
MacAddressString = sprintf('%c%c-%c%c-%c%c-%c%c-%c%c-%c%c', dec2hex(mod(double(Eth0Obj.getHardwareAddress()), 256))');
Temp = Eth0Obj.getInterfaceAddresses.size-1;
IpAddressString = sprintf('%d.%d.%d.%d', mod(double(Eth0Obj.getInterfaceAddresses.get(Temp).getAddress.getAddress), 256));

Image Analyst
Image Analyst 2012년 9월 21일
Is there any way to make a shortcut button called something like "Format Code" which essentially does Control-A (to select all the text in the editor window) followed by Control-I (to fix up the indenting/alignment)? That's a nice set of utility commands that I use often.
  댓글 수: 2
Image Analyst
Image Analyst 2012년 9월 21일
Thanks - it works!

댓글을 달려면 로그인하십시오.

Albert Yam
Albert Yam 2012년 9월 20일
I have a few shortcut buttons. I have the clear all; clc separate from the close all though (and an fclose(all)). I have one specifically for looking at figures, which I use pretty often.
set(gcf, 'Position', [100 50 850 600])
But the most useful one I have is,
addpath (genpath (pwd))
disp('ADD path ''pwd''')
Another 2 I have are savez and diaryz, which just add a time stamp in the filename.

Sean de Wolski
Sean de Wolski 2012년 9월 20일
편집: Sean de Wolski 2012년 9월 20일
  • gohome
%Go home
  • fgcf
I used this through grad school since I would always lose my figures on the Mac. Since starting here, I discovered the very similar shg() and have made the transition.
  • thesaurus
function thesaurus(word)
%SCd 07/19/2010
%Function to look up synonyms to input word.
%How to use:
% thesaurus('useful')
% thesaurus useful
% thesaurus 'useful'
web(['' num2str(word)],'-browser');
  • Sean (my vanity function)
function Sean(varargin)
%Sean is awesome
fprintf('\b is awesome!\n');
Is defaulted in my startup.m file
  • vec
function v = vec(m)
%Helper function to turn a matrix of any size into a column vector using (:)
% This function is meant to make one-line computations easy/fast when
% subscripting already.
%SCd 01/04/2011 (First function of 2011!)
%Updates: -05/24/2011: Used reshape instead of colon for speed.
%Usage: v = vec(m)
% %Original way to one line the median of a slice of 3d matrix:
% M = repmat(magic(200),[1 1 200]); %200x200x200 magic squares
% Mmed = median(reshape(M(:,:,34),[],1)); %34th slice
% %Using the vec() function
% Mmed = median(vec(M(:,:,34)));
%Input Arguments:
% -m: matrix of any size
%Output Arguments:
% -v: m(:)
%See Also: colon reshape
v = reshape(m,numel(m),1);
  • wpwd
function wpwd
  댓글 수: 3
Sean de Wolski
Sean de Wolski 2012년 9월 21일
편집: Sean de Wolski 2012년 9월 21일
@Daniel, so:
It's about 2000x slower on my system:
t1 = 0;
t2 = 0;
for ii = 1:100
v = reshape(m,numel(m),1);
v2 = m((1:numel(m))');
t2 = t2+toc;
Reshape is blazingly fast since it just modifies some header information.

댓글을 달려면 로그인하십시오.

Robert Cumming
Robert Cumming 2012년 9월 20일
I second home - its very useful, the number of times I've had to go to a colleagues screen to help them debug and they have typed clc thinking they are helping me start - but of course that clears the error and any messages on the screen....
My favourite - its not a snippet but a code I would struggle to do wihtout since I work on multiple project is my SaveEditorHistory FEX.

Srinivas 2012년 9월 20일
I have a lazy shortcut to open the current folder I am working in
system(strcat('%SystemRoot%\explorer.exe',' "',pwd,'"'));
  댓글 수: 4
Ashish Uthama
Ashish Uthama 2012년 9월 21일
winopen .

댓글을 달려면 로그인하십시오.

Shane 2012년 9월 21일
I use uigetfile and uigetdir day in and day out, so when I am writing a new code I always add this code to the beginning of my function and I love it:
if isappdata(0,'previousDirectory')
if isnumeric(startDirectory)
[uigf1 uigf2]=uigetfile('*.bdf','SelectFile to Load',startDirectory);
Simply saves my directory location so that I dont start at C:\MyDocuments|MATLAB every time.

Darik 2012년 9월 21일
I'm a clumsy typist...
function hepl (x)
help (x)
  댓글 수: 1
Sean de Wolski
Sean de Wolski 2012년 9월 21일
Tyop corrections in R2012b!!
hepl fmincon
Undefined function 'hepl' for input arguments of type 'char'.
Did you mean:
>> help fmincon
I already highlighted this as my second favorite new feature:

댓글을 달려면 로그인하십시오.


Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by