CONST structure

版本 1.3 (9.5 KB) 作者: Rody Oldenhuis
STRUCT with immutable fields
262.0 次下载
更新时间 2020/5/2

MATLAB's flexible nature is very convenient in most situations. However, this flexibility can also be the cause of hard-to-find bugs.
In many cases, it is desirable to have a data type which is CONST. That is, a datatype which cannot be changed after the first assignment. MATLAB has seen heavy criticism due to its lack of a CONST data type. Although the basic functionality of CONST variables can be achieved through a class with constant properties, that approach leaves much to be desired.

That is where this file comes in. It implements all the functionality of a basic, scalar MATLAB structure, but after a field has been added and assigned a value, that field can no longer be changed.

That means, it strikes a middle ground by offering the flexibility of adding fields dynamically, but disallowing *changing* of fields values.

A simple example session:

C = const; % empty const
C.myField = 'test'; % first assignment; OK
C.myField = 4; % ERROR!

Much more is possible; have a look at the function documentation inside const.m for more information.

引用格式

Rody Oldenhuis (2024). CONST structure (https://github.com/rodyo/FEX-const/releases/tag/v1.3), GitHub. 检索来源 .

MATLAB 版本兼容性
创建方式 R2010a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

无法下载基于 GitHub 默认分支的版本

版本 已发布 发行说明
1.3

See release notes for this release on GitHub: https://github.com/rodyo/FEX-const/releases/tag/v1.3

1.2.0.0

Improved error handling a little bit
Refactored code for better extensibility

1.1.0.0

[linked to Github]

1.0.0.0

要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库
要查看或报告此来自 GitHub 的附加功能中的问题,请访问其 GitHub 仓库