Can you set entries of a table to read-only?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Roel
el 11 de Abr. de 2023
Comentada: Walter Roberson
el 19 de Abr. de 2023
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 comentario
dpb
el 11 de Abr. de 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.
Respuesta aceptada
Divyanshu
el 18 de Abr. de 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 comentarios
Walter Roberson
el 19 de Abr. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Import and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!