Clear Filters
Clear Filters

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

7 views (last 30 days)
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 Comment
dpb
dpb on 11 Apr 2023
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.

Sign in to comment.

Accepted Answer

Divyanshu
Divyanshu on 18 Apr 2023
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 Comments
Roel
Roel on 19 Apr 2023
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 on 19 Apr 2023
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!