What MATLAB functions are underappreciated?

305 ビュー (過去 30 日間)
Chad Greene
Chad Greene 2021 年 4 月 22 日
回答済み: Benjamin Großmann 2021 年 5 月 28 日
Open question: What MATLAB function(s) do you wish you had discovered sooner? Are there any functions that no one talks about, but you use every day?
There are certainly some hidden gems that I don't know about, so rather than leaving it to chance that I might find them someday by accident, share 'em now. I'll get things started with a couple of functions that I find particularly useful.
  1 件のコメント
Stephen23
Stephen23 2021 年 4 月 22 日
I always have the feeling there should be some neat applications for this:

サインインしてコメントする。

回答 (8 件)

Chad Greene
Chad Greene 2021 年 4 月 22 日
One of the most powerful functions I know of took me years from hearing about its existence to using it regularly, because it's not very intuitive to use. But ever since I learned how to think like accumarray, I have felt like I have a kind of power that's usually reserved for gods.
  5 件のコメント
Steven Lord
Steven Lord 2021 年 4 月 22 日
Cell arrays were introduced in MATLAB 5.0 I think. [That was well before I started working at MathWorks.] The accumarray function is newer than cell arrays. I want to say MATLAB 6.5, but I'm not certain.
Rik
Rik 2021 年 4 月 22 日
accumarray is mentioned in the new features summary of R14SP3 ("The accumarray function now allows more flexibility for input/output classes and functions to be called."), but is missing from R13, so I presume it was added in v7.0.

サインインしてコメントする。


Jan
Jan 2021 年 4 月 22 日
編集済み: Jan 2021 年 4 月 22 日
dbstop . Many newcomers mention, that they do not understand, what their code does. The possibility to step through it line by line after setting a break point is essential for professionals also.
Setting conditional breakpoints in all lines of a function, helps to track, where a variable is changed:
function bruteDBOnCondition(mFile, Cond)
% INPUT:
% mFile: Name of the M-file
% Cond: Char vector containing a condition, e.g. x~=0.
% The debugger stops, if the condition is TRUE.
CStr = strsplit(fileread(mFile), '\n');
[~, mName] = fileparts(mFile);
for k = 1:numel(CStr)
if ~isempty(CStr{k}) && ~startWidth(strtrim(CStr{k}), '%')
dbstop('in', mName, 'at', sprintf('%d', k), 'if', Cond)
end
end
end
Further useful applications:
dbstop if error
dbstop if all error % or: if caught error
  4 件のコメント
Rik
Rik 2021 年 4 月 23 日
I should have known there was an easy way to do this. Thanks for the suggestion. This seems to cover all of my use cases, without having to resort to a 100 character line.
Jan
Jan 2021 年 4 月 24 日
It is useful, that clear all does not clear the break points in modern Matlab versions anymore.

サインインしてコメントする。


Chad Greene
Chad Greene 2021 年 4 月 22 日
I rarely see anyone use the lighting functions, but they're incredibly powerful and can make data come to life. They're provide a physical intution not only for surface data, but for almost any type of gridded data. For example, here's a surface without lighting:
figure
surf(peaks(1000))
shading interp
And here's the same surface with lighting:
figure
surf(peaks(1000))
shading interp
camlight
material dull
  1 件のコメント
Sean de Wolski
Sean de Wolski 2021 年 4 月 22 日
I like the actual light object. It's actually fairly intuitive "I want to put a green light here" and allows you do cool things like the mathworks logo (>>edit logo)

サインインしてコメントする。


Sean de Wolski
Sean de Wolski 2021 年 4 月 22 日
onCleanup - I want to reliably do this thing at the end... I've actually gotten to the point that I will pass these (or cell arrays on them) as return outputs to functions or properties of a class as a means to save something as long as necessary. E.g:
[filename, deleter] = someFunctionThatWritesATempFile(stuff)
filename = tempname+".csv";
writetable(stuff, filename)
deleter = onCleanup(@()delete(filename))
Now if I want the file to be temporary - just call it without an input. If I need that file to exist until I'm done with it (e.g. report gen output or something, keep the deleter around and then it still goes away at the end).

Bjorn Gustavsson
Bjorn Gustavsson 2021 年 4 月 22 日
Just a couple of basic ones:
  • lscov - linear regression when there is a data-covariance-matrix
  • svd (to use together with Tikhonov-regularization instead of pinv - every time!)
Of of the file exchange:
To be continued...
  1 件のコメント
Steven Lord
Steven Lord 2021 年 4 月 22 日
If you like the factorize File Exchange submission, you may be interested in the decomposition function that has been included in MATLAB for several releases.

サインインしてコメントする。


Sean de Wolski
Sean de Wolski 2021 年 4 月 22 日
編集済み: Sean de Wolski 2021 年 4 月 22 日
mfilename I use this all of the time for building relative file or folder paths. I want currentProject().RootFolder to replace it as that's generally better but that's not supported in Compiler workflows so mfilename it continues to be.
  1 件のコメント
Jan
Jan 2021 年 4 月 22 日
Yes, this has been useful for projects, which stores data in subfolders of the M files:
myPath = fileparts(mfilename('fullpath'))

サインインしてコメントする。


Sean de Wolski
Sean de Wolski 2021 年 4 月 22 日
tempname/tempdir - Need to write something and guarantee write permissions on any platform...
  1 件のコメント
Rik
Rik 2021 年 4 月 22 日
The only downside is that it isn't persistent, which is where this comes in.

サインインしてコメントする。


Benjamin Großmann
Benjamin Großmann 2021 年 5 月 28 日
arguments declaration within functions, introduced in 2019b, is very powerful (pythonic? ;-)) for writing generic and reusable functions. Argument validation is great, but I really enjoy the possibility to define (additional) optional name-value argumentsn as well as the functions usage with name-value pairs and the possibitlity to define default values for input arguments.
function someFunction(invars)
arguments
invars.someInt {mustBeInteger} = rand(3);
invars.someText {mustBeText} = inputdlg('Input argument b');
end
% use invars.someInt and invars.someText within the function
% someCode
%
end
Call the function with or without any of the (optional) arguments:
someFunction(someInt=2, sometText='Text');
someFunction(someInt=2);
someFunction(sometText='Text');
someFunction();

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by