Can you set entries of a table to read-only?

1 回表示 (過去 30 日間)
Roel
Roel 2023 年 4 月 11 日
コメント済み: Walter Roberson 2023 年 4 月 19 日
I'm trying to use matlab to save some measurement data, I want to save the data to a table and make certain entries to be read-only such that they can never be overwritten. Is there a function that does this?
  1 件のコメント
dpb
dpb 2023 年 4 月 11 日
No. MATLAB has no constant data class. It shouldn't be hard to ensure your code never addresses given variables, however, but if it's just a value scattered around here and there and other stuff around it is routinely modified, an indexing error would be somewhat more difficult to code against.
Describe the overall situation more in why you have concerns that you might be changing these.

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

採用された回答

Divyanshu
Divyanshu 2023 年 4 月 18 日
As of now there is no direct way to apply restrictions on the table or the specific entries using MATLAB. But you can try this workaround if it works for your case,
You can have a look at the below script which creates a sample table, and a corresponding csv file from the table.
A file is created from the table using writetable function, the write access from the file is removed using fileattrib function, which means it can be only read and cannot be edited.
LastName = ["Sanchez";"Johnson";"Zhang";"Diaz";"Brown"];
Age = [38;43;38;40;49];
Smoker = [true;false;true;false;true];
T = table(LastName,Age,Smoker);
writetable(T, 'data.csv');
fileattrib('data.csv', '-w');
The above workaround is going to restrict the write-access of entire file and is not going to provide control of access up to each cell of the table.
Moreover, it is a quite specific scenario when you want to control some random cells to be only read and while others can be modified, this would be complex to code without having detailed information about the requirement.
Please refer the following documentation for more information:
  2 件のコメント
Roel
Roel 2023 年 4 月 19 日
I might use this thanks. Always handy to save data in multiple locations with different file formats.
To give a little more context, I'm making a 'database' class in MATLAB in which I save my raw data and analyze it using various functions. I want to save the raw data in a table allong with some parameters so I can always reset it to its original form.
To refrain me or anyone else to change the raw data I want it to be read only.
Walter Roberson
Walter Roberson 2023 年 4 月 19 日
It is not possible to set variables of a table() class to be read-only. You would have to create your own class for that, in which case enforcement would be up to your own methods.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by