General
Follow


goc3

Don't Reinvent the Wheel

goc3 on 13 Oct 2023
Latest activity Edit by Stephen23 on 6 Nov 2023

Have you ever learned that something you were doing manually in MATLAB was already possible using a built-in feature? Have you ever written a function only to later realize (or be told) that a built-in function already did what you needed?
Two such moments come to mind for me.
1. Did you realize that you can set conditional breakpoints? Neither did I, until someone showed me that feature. To do that, open or create a file in the editor, right click on a line number for any line that contains code, and select Set Conditional Breakpoint... This will bring up a dialog wherein you can type any logical condition for which execution should be paused. Before I learned about this, I would manually insert if-statements during debugging. Then, after fixing each bug, I would have to delete those statements. This built-in feature is so much better.
2. Have you ever needed to plot horizontal or vertical lines in a plot? For the longest time, I would manually code such lines. Then, I learned about xline() and yline(). Not only is less code required, these lines automatically span the entire axes while zooming, panning, or adjusting axis limits!
Share your own Aha! moments below. This will help everyone learn about MATLAB functionality that may not be obvious or front and center.
(Note: While File Exchange contains many great contributions, the intent of this thread is to focus on built-in MATLAB functionality.)
Matt J
Matt J on 15 Oct 2023 (Edited on 15 Oct 2023)
My "Aha" would probably have to be matchpairs.
On an separate unrelated note, this thread has inspired me to open Do Reinvent the Wheel
Catalytic
Catalytic on 14 Oct 2023 (Edited on 14 Oct 2023)
For years, I was numbering my matrices manually in a most tedious fashion , like -
A1=1;
A2=2;
A3=3;
-
-
-
A1000=1000;
Then, Aha! I discovered EVAL and it got so much simpler!!!
for i=1:1000
is=num2str(i);
eval(['A' is '=' is]);
end
Stephen23
Stephen23 on 6 Nov 2023 (Edited on 6 Nov 2023)
"For years, I was numbering my matrices..."
Whatever way you do it, forcing meta-data (e.g. pseudo-indices) into variable names is an inefficient use of MATLAB**. In fact, the MATLAB documentation has an entire page specifically advising against what you are doing: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
"... manually in a most tedious fashion"
Computers are really good at doing very simple tasks repeatedly in loops. So when you sit and repeat some simple task then you are just doing the computer's job for it.
"I discovered EVAL and it got so much simpler!!!"
Using EVAL like that forces you into writing slow, inefficient, obfuscated, complex, hard-to-debug code. And yet is easily avoided by using very basic indexing.
** which is based around arrays and indexing, not on having lots and lots and lots of separate variables in the workspace. So rather than forcing pseudo-indices into variable names and writing slow, complex, inefficient, obfuscated, buggy code that relies on evil EVAL... you should use indexing. Just like MATLAB was written for.
Adam Danz
Adam Danz on 16 Oct 2023
I'd like to echo what @Walter Roberson said.
It would be much better to use indexing in this case where all values are stored in a vector. Instead of a variable a23 you could just use a(23).
Walter Roberson
Walter Roberson on 15 Oct 2023
goc3
goc3 on 14 Oct 2023
While I also created variables using eval() in the past, I learned that a better way is to dynamically name struct fields. I would recommend that you try this approach:
S = struct;
for i = 1:1000
S.("A" + i) = i;
end
Stephen23
Stephen23 on 6 Nov 2023 (Edited on 6 Nov 2023)
"While I also created variables using eval() in the past, I learned that a better way is to dynamically name struct fields."
Much better is to use actual indexing.
Rather than inefficiently forcing pseudo-indices into fieldnames, it is much better to use actual indices (no superfluous type conversion, more efficient), e.g. into a numeric array, a cell array, a structure array, a table, etc.
Harnessing the array processing power of MATLAB is the way to go.
David
David on 16 Oct 2023
Great tip!
John D'Errico
John D'Errico on 13 Oct 2023
I think features like ComparisonMethod are easy to miss. They get added to existing functionality, extending those tools in many useful ways. But unless we carefully read the release notes for each release (something I am often lax to do) then we never see these nice additions to the language. The answer of course is to read the release notes.
Walter Roberson
Walter Roberson on 13 Oct 2023
xline() and yline() were added in R2018b. In older versions, there was refline (though I guess that was Statistics toolbox)
Adam Danz
Adam Danz on 13 Oct 2023 (Edited on 13 Oct 2023)
Great topic!!
I remember discovering the "ComparisonMethod" option in functions like max, min, issorted, and others. It's really useful when you're only concerned with magnitudes and not signs.
For example,
a = [4 2 -5 -3 1]
[m, n] = max(a,[],ComparisonMethod='abs')
m =
-5
n =
3
It save users from thinking about how to solve this by using abs() without losing the original sign of the max(abs()).
aAbs = abs(a);
[~, n] = max(aAbs)
m = a(n)
n =
3
m =
-5
>>