The MATLAB Blog

Practical Advice for People on the Leading Edge

Two of my ‘White Whale’ features have been added to MATLAB R2024a

Everyone has them! MATLAB features that you've been requesting for ages. You email MathWorks support, you post long rants on discussion forums, you even join MathWorks and spam the internal bug/feature request database before turning up to the office and sitting on the developer's desk to make your case. That last one might just be me though!
Every six months, you scan the release notes looking to see if they are there. Sure, there are 1,914 new features in R2024a but is what I want in there? My obsessions? My 'white whales'?
0m2zyki5.png

Spell checking in MATLAB Editor and Live Editor

I've been working in technical computing for almost 25 years so you may think that the sort of functionality I'm desperate for is related to advanced mathematics or high performance computing. I have some desires there, to be sure, but since I use MATLAB Live Scripts every day, including to write the articles on this blog, one of the desires that burned in my soul is rather more mundane. I wanted spell checking.
There's actually a programmatic way of doing doing a spell check via Text Analytics Toolbox using the correctSpelling function that has been available since R2020a:
% Requires Text Analytics Toolbox
str = [
"A documnent containing some misspelled worrds."
"Another documnent cntaining typos."];
documents = tokenizedDocument(str)
documents =
2×1 tokenizedDocument: 7 tokens: A documnent containing some misspelled worrds . 5 tokens: Another documnent cntaining typos .
updatedDocuments = correctSpelling(documents)
updatedDocuments =
2×1 tokenizedDocument: 7 tokens: A document containing some misspelled words . 5 tokens: Another document containing typos .
While this is very cool and useful in a different context, it's not quite what I had in mind. I just wanted my misspelled words to be underlined so that I can click on them and get them corrected. I'm a simple man with simple needs. I'm not the only one. MATLAB Central user, Alex Pedcenko also wanted spell checking. This is just the tip of the iceberg. Spell checking in MATLAB has been requested a lot!
Of course, you know where I'm going. As of R2024a, MATLAB has spell checking. It's not switched on by default though. In Live Editor, you need to click on the View tab and click on Spelling.
Spellingon.png
It may seem simple but this is a gamer-changer for me!

Local functions (almost) anywhere

Way back at the beginning of my MathWorks career, I wrote a guest blog post for Loren Shure's The Art of MATLAB. It was great training for me and the first step in Loren handing the MathWorks blogging baton to me. I, however, was a little upset. I was upset because I couldn't define a local function in the middle of a script, like this:
function result = MyIsPrime(n)
% Function to check if n is prime
if n <= 1
result = false;
return
end
q = floor(sqrt(n));
n = uint32(n);
for i = uint32(2):q
if mod(n,i) == 0
result = false;
return
end
end
result = true;
end
 
%Run the function
MyIsPrime(29)
ans = logical
1
In previous versions of MATLAB, all local functions had to be at the end of a script. You couldn't define them anywhere you wanted unless you used anonymous functions. This led to what I call the 'Helper function anti-pattern' where blog posts and live scripts containing demonstrations would have a bunch of functions at the very end with a title like 'Helper functions' or 'Support functions' or something similar. Sure, I could just define them as separate .m files but then the reader of my document wouldn't be able to see them. This limitation bothered me because it inhibited how I taught or told stories.
Now, you can define them almost anywhere you like. This is a major quality of life change for me and I have been heaping praise on the internal team who did the extensive engineering behind the scenes to make it happen. I've been saying almost anywhere though. So where can't you define functions?
You still can't define functions in for-loops or conditionals. So, for example, the following will not work
if 1
function inConditional(x) % <- Error, not top level
end
end
This restriction is a deliberate design decision and is done to preserve scope and declaration clarity. Placing function definitions inside conditional blocks can obscure the function's scope, leading to uncertainty about its availability outside the conditional block and the specific conditions under which it is defined. This restriction is enforced to safeguard the code's clarity and straightforwardness.

When does an .m file become a script?

This update changes the rules (a bit) for what makes an .m file a script. Before, if you opened an .m file and saw that the top contained a function definition then you knew it was a function file. Now, a script is any code file that contains a MATLAB command or expression that is outside the body of a function. For example, I could have any number of functions at the top of my file but as soon as I add a single line of code anywhere in the file that is not inside one of those functions, it becomes a script whereas previously you would have got an error message. Once it becomes a script, the function at the top is only usable from inside that script since it is now a local function. That is, it's no longer a function file but a script file.

What are your 'White Whale' features?

R2024a has made me much happier now that two of my 'White Whale' features have been implemented. What are yours though? Join us in the new Ideas channel in the Discussions area of MATLAB Central and tell us about the features you wish MATLAB had.
|
  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.