Can you set entries of a table to read-only?
8 views (last 30 days)
Show older comments
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
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.
Accepted Answer
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
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.
More Answers (0)
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!