Update: This submission is no longer available on the File Exchange.
Jiro's pick this week is RENVAR by Duane Hanselman.
Imagine that you defined a variable:
oldVar = magic(3);
Now you want to rename this variable because the meaning of it has changed. How do you do it? Do you do it interactively from the Workspace Browser?
Or perhaps programmatically:
newVar = oldVar;
clear oldVarDuane's renvar let's you do this in a single swoop.
renvar newVar veryNewVar
If you look inside his function, it is basically executing the two lines from above. I like this for a couple of reasons:
- One line is shorter than two.
- It has a nice nugget of information regarding memory allocation.
From the H1 line:
RENVAR Rename Variable Without Memory Reallocation.
Yes, in fact this method (as well as the two-liner) does not reallocate memory. No temporary memory is used, because it is doing "lazy copying". So there's no need to worry even if you are renaming a very large variable. Take a look at Loren's blog posts on Memory Management for Functions and Variables to learn more about "lazy copy".
Comments
Over the years, I have created many few-line functions, just to make my life easier. What are some of your small, but useful functions? Tell us here.
Get
the MATLAB code
Published with MATLAB® 7.6


Hello Jiro,
That was a nice,simple, and informative post.
When does lazy copying become a factor in practice?
As far as nicknames, how does “Dig Doug” sound? Or maybe “Doug the Destroyer”? Or my personal favorite, “Doug Diesal”
Hi Quan,
There are couple of situations I can think of for lazy copying. One is when you are creating copies of variables:
a = rand(1000);
b = a;
c = a;
Although there are three 1000-by-1000 matrices, there’s only one of them in memory. The other two are simply referencing the one. As soon as you modify the variables, then actual physical copies are made.
Another one is when you are passing a variable into a function. Even though a function has its own workspace, if the input variable being passed is only being referenced (not modified), it is not being copied (see the first example in Loren’s post).
Hi Doug, I guess everyone uses few-liners. Here are some of mine. Nothing worth to put on Matlab Central, but you asked for it…
Regards
Markus
function fileSize = getfilesize(fileName)
dirStruct = dir(fileName);
if ~isempty(dirStruct)
fileSize = dirStruct.bytes;
else
fileSize = [];
end
function filename = chomppath(str)
%CHOMPPATH Return file name wihtout path.
str = strrep(str, ‘\’, ‘/’);
[pathname, filename, extension] = fileparts(str); %#ok
filename = [filename extension];
function str = concatpath(varargin)
%CONCATPATH Concatenate file parts with correct file separator.
str = ”;
for n=1:nargin
curStr = varargin{n};
str = fullfile(str, chompsep(curStr));
end
str = strrep(str, ‘\’, ‘/’);
function nowinseconds = mbtime
%MBTIME Return serial date number converted to seconds
% function datenummx is a mex-file found in toolbox/matlab/timefun
nowinseconds = datenummx(clock)*86400;
function deleteallwaitbars
%DELETEALLWAITBARS Delete all existing waitbars.
curShowHiddenHandles = get(0, ‘ShowHiddenHandles’);
set(0, ‘ShowHiddenHandles’, ‘on’);
ch = get(0, ‘Children’);
for k = 1:length(ch)
if strcmp(get(ch(k), ‘Type’), ‘figure’) && …
strcmp(get(ch(k), ‘Tag’), ‘TMWWaitbar’)
delete(ch(k));
end
end
set(0, ‘ShowHiddenHandles’, curShowHiddenHandles);
function deleteallfigures
%DELETEALLFIGURES Delete all existing figues.
close all
close all hidden
drawnow
ch = get(0, ‘Children’);
for n=1:length(ch)
if strcmp(get(ch(n), ‘Type’), ‘figure’)
delete(ch(n));
end
end
drawnow
function spyMatrix = cellspy(cellMatrix)
%CELLSPY Return sparsity pattern for cell arrays.
% Y = CELLSPY(X) returns a matrix of the same size as cell array X with
% zeros at the positions of empty cells and ones for non-empty cells.
spyMatrix = ~cellfun(@isempty, cellMatrix);
if nargout == 0
spy(spyMatrix);
axis normal;
end
function check = yesnoinput(str)
%YESNOINPUT Force user decision.
% YESNOINPUT(STR) diplays string STR and waits for the user to type ‘y’
% for yes or ‘n’ for no. true is returned for yes and false for no.
while 1
answer = input([str,' (yes/no)\n'],’s');
if ~isempty(answer)
if strcmpi(answer, ‘y’) || strcmpi(answer, ‘yes’)
check = true;
return
elseif strcmpi(answer, ‘n’) || strcmpi(answer, ‘no’)
check = false;
return
end
else
disp(‘Incorrect input. Try again’);
end
end
function check = yesnodialog(question, windowtitle, defaultAnswer)
%YESNODIALOG Open window for user decision.
if ~exist(‘windowtitle’, ‘var’) || isempty(windowtitle)
windowtitle = ‘Question dialog’;
end
if ~exist(‘defaultAnswer’, ‘var’)
defaultAnswer = ‘yes’;
end
while 1
answer = questdlg(question,windowtitle,’yes’,'no’,defaultAnswer);
switch answer
case ‘yes’
check = true;
return
case ‘no’
check = false;
return
end
end
script profileon:
profile off
profile clear
profile on -detail builtin
script profilereport:
profile report
profile off
Here is one I use very frequently
script ccc
%
clear all
delete(allchild(0)) % stronger than close all
clc
I always work with magnitude of FFTs, and due to how MATLAB organizes data in the FFT, I created this wonderful one-liner:
function img = magFFT2(kspace)
img = abs(fftshift(fftn(fftshift(kspace))));
end
Thanks everyone for your one (few)-liners.
Markus, I like your “yesnoinput” and “yesnodialog”. These interface functions (input, questdlg, etc) are already easy to use, but by creating wrapper functions, it makes them even more user-friendly.
Eric, I also have my own “ccc” command which does all of that. I use “close all force” for my closing function
Luca, nice use of MATLAB to give you what you want! MATLAB has its way of returning the results, and you have your way, and they may not always be the same. But with this, everyone’s happy!
dear professor:
I am a student of a university. I do not know how to calculate the multi-dimensional fast fourier transform using fftn funcion. I want to know that whether ” fftn(rand(2,2,3)) “can give me the results of 3 dimensional fourier transform?
jinshan,
This is off-topic, and I don’t have an answer for you.
I suggest you take a look at the documentation for the functions you are interested in. If you have specific questions, you can post your questions on the newsgroup
How can I get the RENVAR function at the moment?