Dumb mistakes we make with MATLAB.

Matt Fig on 22 Feb 2011
Latest activity Reply by Bruno Luong on 1 Jun 2022

O.k., admit it. Who has done this (or something that resulted in the same loss of info) before?
>> laster
??? Undefined function or variable 'laster'.
>> lasterr
ans =
Undefined function or variable 'laster'.
D'Oh! I need a ERRORBEFORELASTERR function.
What's one of your dumb MATLAB mistakes?
Bruno Luong
Bruno Luong on 1 Jun 2022
Use 'end' to index an empty array.
Bruno Luong
Bruno Luong on 1 Jun 2022
a = [];
a(end) = 3
Array indices must be positive integers or logical values.
ugly work around
a = [];
a(max(1:end):end) = 3
DGM
DGM on 1 Jun 2022
I spent a moment today trying to figure out why
outarray = logical(size(inarray)); % preallocate
didn't create a logical array with the same size as inarray.
I swear I must have brain damage, and with the caliber of forehead slap I needed, I can see how it would happen.
Michael Richards
Michael Richards on 15 Sep 2020
fig = figure('CloseReqFcn',@error_prone_fcn)
presses x button
error
alt-f4
error
tries close matlab
error
task manager > end task
error
Steven Lord
Steven Lord on 15 Sep 2020
Specifying 'force' with the close command will force the figure to close, bypassing its CloseRequestFcn.
fig = figure('CloseRequestFcn',@(varargin) error('No closing this figure!'));
close(fig) % Will not close the figure
close(fig, 'force') % Will close the figure
fig % This is now a handle to a deleted figure
Jan
Jan on 3 Oct 2017
I needed a short and confusing command for a demonstration:
eval = 1:255
eval eval
Error: "eval" was previously used as a variable, conflicting with its use here as the name of a function or command.
This works:
eval('eval')
>> 100 117 96 107
Adam
Adam on 21 Sep 2016
Since I happened across this old thread, one that continually catches me out since I work a lot with plotting of complex signals is to the effect of
figure; plot( mySignal )
hilbSignal = hilbert( mySignal )
hold on;
plot( hilbSignal );
Doh! Pretty spider's web, but not quite what I was looking for. One day I will remember I have complex data and that I actually want either the real or imaginary part! In this case I keep just thinking of the hilbert transform result as the imaginary part only that it adds.
Kye Taylor
Kye Taylor on 24 Apr 2013
>> close al
Error using close (line 111)
Specified window does not exist.
Dammit!
Knut
Knut on 5 Mar 2013
for i = 1:3
disp(exp(i*pi));
end
Instead of:
for i = 1:3
disp(exp(1i*pi));
end
Geoff
Geoff on 10 May 2012
Corrupting years of experience in other languages...
After using MatLab for several months now, I find that when I go back to writing C code I catch myself forgetting to put parentheses on my if statements and using single-quotes for my strings.
Sean de Wolski
Sean de Wolski on 10 May 2012
It still does!
Daniel Shub
Daniel Shub on 10 May 2012
I believe the name at one point meant Matrix Laboratory
Jan
Jan on 8 May 2012
The strings 'default', 'remove', and 'factory' are not allowed as uimenu labels, see Ref: uimenu_props. To get the string 'default' as label, '\default' must be used.
This is another example for the bad magic strings. Imagine you want to create a bunch of uimenu s automatically using a list of words. Then the exception handling for these three words will be ugly and increase the program size without a real benefit.
[EDITED] The named strings have a special meaning for other handle graphics objects also, e.g. UICONTROLs:
uicontrol('String', 'default')
This doe not display 'default' in a button, but an empty string, because this is the default value:
get(0, 'defaultUIControlString')
To display 'default', 'factory', or 'remove', a leading backslash is required, see doc: setting-default-property-values:
uicontrol('String', '\default')
It would be smarter to use the backslash for the special commands, but this cannot be changed without breaking the backward compatibility.
Walter Roberson
Walter Roberson on 24 Apr 2013
Maybe \\default ?
Jan
Jan on 24 Apr 2013
What is required to display the string '\default' in an UICONTROL???
Jan
Jan on 4 Feb 2012
Although I never use clear all (see Answers: Bad CLEAR ALL), it appears in user-supplied code frequently. If all is defined as a variable, the behaviour might be unexpected:
clear all
b = 1;
all = 2;
clear all
whos
% Name Size Bytes Class Attributes
% b 1x1 8 double
If "all" is a variable, it is cleared, but the other variables are not touched.
[EDITED] The "clear all" behaviour is a typical example for the bad programming practize of using magic numbers. Here the string 'all' is magic, because it triggers a totally amnesia, if there is no variable of this name. It would be smarter to use invalid symbols for special commands, e.g. "$all". See Wiki: magic numbers and Wiki: magic strings.
Eric Sampson
Eric Sampson on 24 Apr 2013
P.S. I've also recently requested that TMW consider making the use of 'clear all' an M-lint/Analyzer warning, because it has side effects that trip people up (like clearing breakpoints & globals, flushing the M-file cache, etc). If you like the idea, call/email support and lend your vote to this enhancement request! :)
Eric Sampson
Eric Sampson on 24 Apr 2013
To be honest I think a lot of the functions that were created in early MATLAB versions are the ones that tend to have syntax issues like this... Since they've been around so long it would be hard for TMW to break backwards compatibility. As TMW hired more professional programmers over the years, instead of mathematicians/scientists doubling as programmers, things have improved IMHO, like your example about '-regexp'. Of course this is all personal opinion YMMV etc, and it's not intended to be a slight at all to TMW old-timers :)