Trending Discussions

Jan Studnicka
Jan Studnicka
Last activity on 10 Jul 2026 at 17:59

Did you know that function double with string vector input significantly outperforms str2double with the same input:
x = rand(1,50000);
t = string(x);
tic; str2double(t); toc
Elapsed time is 0.276966 seconds.
tic; I1 = str2double(t); toc
Elapsed time is 0.244074 seconds.
tic; I2 = double(t); toc
Elapsed time is 0.002907 seconds.
isequal(I1,I2)
ans = logical
1
Recently I needed to parse numbers from text. I automatically tried to use str2double. However, profiling revealed that str2double was the main bottleneck in my code. Than I realized that there is a new note (since R2024a) in the documentation of str2double:
"Calling string and then double is recommended over str2double because it provides greater flexibility and allows vectorization. For additional information, see Alternative Functionality."
I have been a loyal MATLAB user for 25 years, starting from my university days. While many of my peers migrated to Python, I stayed for the stability, compatibility, and clean environment. However, I am finding the 2025 version exceptionally laggy. Despite running it on an $10k high-end machine, simple tasks like viewing variables and plotting take up to 60 seconds - actions that were near instantaneous in the 2020 version. I want to stay continue with MATLAB, but this performance gap is a major hurdle and irritation. I hope these optimization issues can be addressed quickly.
Many widely cited code style guides originate from large-scale software engineering contexts: multi-developer teams, large codebases, separate reviewers, and tooling-driven workflows. While those constraints are valid in their domain, they often map poorly onto scientific and engineering scripting as it is typically practiced with MATLAB.
In laboratory and engineering environments, code serves a different role. It is frequently written by individuals or small groups, and then iteratively modified, copied, adapted, and extended as part of an evolving problem-solving process. In this context, the primary priorities are not strict stylistic consistency or tooling compatibility, but rather:
  • maintaining clarity of underlying structure,
  • minimizing the risk of errors during modification, and
  • supporting rapid comprehension of mathematically or logically dense code.
This raises the question: should fixed line-length limits be replaced by context-aware principles? Could these be supported by a suitable AI tool?
The following proposal outlines a small set of heuristics governing line length, based on observations of real-world MATLAB usage, particularly for numerically intensive and structurally rich code. These heuristics aim to:
  • preserve and expose meaningful structure (e.g. systems of equations, tables, repeated patterns)
  • avoid formatting that obscures relationships or introduces errors, and
  • treat different kinds of code (logic vs. data vs. structured expressions) appropriately.
Scope
These principles apply to scientific and engineering scripting, particularly:
  • MATLAB-like environments
  • numerically or structurally dense code
  • monolithic or semi-monolithic workflows
  • code that is frequently modified, copied, and adapted
They are not intended for large-scale commercial software engineering, where different constraints dominate.
Core Objective
Line length and formatting should maximize comprehension, structural clarity, and correctness under modification, rather than enforce arbitrary limits.
Hierarchy of Heuristics
Higher-numbered heuristics take precedence over lower-numbered ones.
1) Reasonable Line Length
Code intended for reading should use a reasonable line length, guided by:
  • human visual comprehension when scanning
  • clarity of expression
  • preservation of logical units
This would tend toward 70-100 characters per line, depending on the density.
2) Preserve Semantic Integrity of Lines
Line breaks must not split code in ways that degrade understanding.
Avoid:
  • dangling fragments
  • very short continuation lines
  • separation of tightly coupled elements
  • etc.
Prefer:
  • keeping logically cohesive expressions intact
  • breaking only at clear structural boundaries
One slightly longer line is preferable to two poorly structured lines.
3) Treat Data as Data (Not Prose/Code)
Code that primarily represents data rather than logic is not intended for sequential reading.
This includes:
  • large numeric vectors
  • lookup tables
  • pasted datasets
  • etc.
Such code:
  • may exceed line length limits without restriction
  • should prioritize density and structural stability
  • is assumed to be accessed via search or indexing rather than visual parsing
Readability is not the objective; retrievability and integrity are. Yes, this intentionally rejects the enterprise concept that data must be separate from code, instead replacing it with the concept that the IDE should support what some real-world users actually use, for example by formatting/aligning/showing data differently.
4) Preserve and Expose 2D Structure
If code encodes a logical, mathematical, or tabular structure with inherent spatial relationships, it should be represented accordingly.
This includes:
  • systems of equations
  • tabulated data
  • repeated structured expressions
  • etc.
Requirements:
  • alignment should be used where it improves comprehension
  • patterns should be visually apparent
  • deviations from patterns should be easily detectable
This principle should be applied strongly, tending toward mandatory use where feasible.
Exception
If a structure would become impractically wide, a compromise representation may be used.
Breaking meaningful spatial structure is considered harmful to comprehension and correctness.
5) Preserve Structural Consistency Across Similar Code
Code segments representing similar or related logic should be expressed in consistent structure and layout.
This applies to:
  • repeated formulas
  • analogous computations
  • structurally similar transformations
  • etc.
Consistency enables:
  • rapid comparison
  • detection of inconsistencies
  • safer modification
Similar logic should be represented in similar ways.
Meta-Principles
A. Structure Over Style
Line lengths should reflect the underlying structure of the problem, not conform to arbitrary limits.
B. Correctness Over Convention
Avoid line lengths and formatting that:
  • obscures patterns
  • hides inconsistencies
  • increases the risk of modification errors
C. Optimize for Modification
Code in this domain is frequently:
  • edited
  • duplicated
  • adapted for n
  • extended
  • commented-out for testing different versions
  • etc
Line lengths should reduce the likelihood of errors during these operations, for example by keeping atomic concepts on the same line rather than splitting them up.
D. Anomaly Visibility
Formatting should make unexpected deviations immediately visible.
E. Tool Support
An intelligent tool should:
  • respect and preserve structural layout
  • avoid rigid line-length enforcement
  • detect patterns and inconsistencies
  • assist rather than constrain the programmer
I would be interested to hear how well these ideas match others’ experience, particularly in scientific or engineering workflows.
See also:
Ralf Axel
Ralf Axel
Last activity on 8 Jul 2026 at 4:47

Hallo zusammen, Ich habe einen Frage zu meinen Programm. Dies will einfach nicht laufen und ich finde keinen Fehler mehr. Ich habe mein Programm bei Simulink geschriebenen den Code bei Maltab Function. Das Board ist ein Adruino Uni Board. Ein Ultrasonic Sensor soll die Füllstände ich Wäschekörben messen. Dabei wird unter voll oder halbvoll entschieden. Anschließend wird ein Motor angesprochen, der entweder 15 oder 30 Sekunden laufen soll. Überwacht wird der Motor von einem Thermistor (den habe ich hier PT100 genannt) und einen Vibrationsschalter. Dazu soll der Vibrationsschalter über einen Resetknopf zurückgesetzt werden. Ich hoffe ihr könnt mir weiterhelfen. Vielen Dank:) if true % code end
n= input('Escolhe um número inteiro postivo. ')
primo=true;
i=2;
while i<n
if mod(n,i)==0;
primo=false;
end
i= i+1;
end
if primo && n>1;
disp('É primo')
else
disp('Não é primo')
end
anterior= n-1;
while true
primo=true;
i=2;
while i< anterior
if mod(anterior,i)==0;
primo= false;
end
i= i+1;
end
if primo && anterior>1;
end
anterior= anterior-1;
end
disp(anterior)
seguinte= n+1;
while true;
primo= true;
i=2;
while i<seguinte;
if mod(seguinte,i)==0;
primo=false;
end
i=i+1;
end
if primo && seguinte>1;
end
seguinte= seguinte+1;
end
disp(seguinte)
Any ideas? It is in portuguese if you intend to translate it.
How does everyone use MatLab right now? I can't think of any ideas what i can use this software for!
Hi everyone
It is my pleasure to be able to report on a project that several teams at MathWorks have been working on for some time now. A new object management system that promises to make object oriented code in MATLAB a lot faster.
The new system is available as a limited beta in the pre-release of MATLAB 2026b. It is not turned on by default. If you are developing OOP code, we'd love you to try it out. Most of the time, no code changes will be necessary but there are a small number of well-defined case where you will need to update your code.
The team are currently looking for MATLAB developers to work with who would like to try this out.
More details, including how to join the beta, are available in the following blog post https://blogs.mathworks.com/matlab/2026/07/14/objects-are-about-to-get-much-faster-in-matlab/
Best wishes,
Mike

About Discussions

Discussions is a user-focused forum for the conversations that happen outside of any particular product or project.

Get to know your peers while sharing all the tricks you've learned, ideas you've had, or even your latest vacation photos. Discussions is where MATLAB users connect!

More Community Areas

MATLAB Answers

Ask & Answer questions about MATLAB & Simulink!

File Exchange

Download or contribute user-submitted code!

Cody

Solve problem groups, learn MATLAB & earn badges!

Blogs

Get the inside view on MATLAB and Simulink!

AI Chat Playground

Use AI to generate initial draft MATLAB code, and answer questions!