Hello,
This is a screenshot of my MAT-file. How do you permantly delete certain structures? For example, if I want 'B' deleted.
I used to be able to right click on the one I wanted to delete, and it gave me options to delete, duplicate, move, etc, but one day it stopped letting me do that. Does anyone know why it no longer lets me delete right in that window? Is there something I need to enable?
Thanks!

 Respuesta aceptada

Matt J
Matt J el 15 de Ag. de 2020
Editada: Matt J el 15 de Ag. de 2020

1 voto

I don't remember that ever being possible through the Matlab GUI, but attached is a MEX file that can do it.
rmvarMatfileMEX('allData.mat','B')

10 comentarios

Walter Roberson
Walter Roberson el 15 de Ag. de 2020
If I recall properly, in v5 .mat to v7 .mat files, it is possible to mark a space as unused, effectively deleting it, but that the mechanism does not reclaim space.
Sclay748
Sclay748 el 17 de Ag. de 2020
I tried this one. Thought it was working because it was deleting the variables in the window, but when I went to save the .mat file, they all came back.
Matt J
Matt J el 17 de Ag. de 2020
If you saved the variables to the .mat file again, of course they came back... The idea is to not put the variables back in the .mat file once you've removed them.
Sclay748
Sclay748 el 17 de Ag. de 2020
But they are not being called/used in the script anywhere. Usually they only come back if they are in use, but the script changed and they are no longer used anywhere. Am I incorrectly assuming that?
Matt J
Matt J el 17 de Ag. de 2020
Editada: Matt J el 17 de Ag. de 2020
Do the following test and tell us the result,
clear all
rmvarMatfileMEX('allData.mat','B')
load allData
whos B
Sclay748
Sclay748 el 17 de Ag. de 2020
That still didnt work for me, but I did get it to work. I did a combination of:
rmvarMatfileMEX('allData.mat','B')
then at the bottom of the script:
clearvars B
Then it deleted B and I was able to save.
Matt J
Matt J el 17 de Ag. de 2020
Editada: Matt J el 17 de Ag. de 2020
What was the result of running the 4 lines of code that I posted in my last comment?
Your use of clearvars has nothing to do with the issue in your posted question. Your question is about removing B from a .mat file, not removing it from the Matlab wokspace. It is obvious and trivial that the .mat file will never contain B if you clear B before the .mat file is created. Your question is about removing B from the .mat file when it has already been put there by mistake.
Sclay748
Sclay748 el 17 de Ag. de 2020
It cleared B, but then the script said did not recognize a variable that was actually used and still included in the .mat file.
Matt J
Matt J el 17 de Ag. de 2020
Well, you need to show the full script in order for us say what's really going on. It sounds unrelated, though, to the issue in your post.
Sclay748
Sclay748 el 18 de Ag. de 2020
Unfortunatly I cant show it due to it being for work, but atleast the problem has fixed itself and the variables are deleting as expected. I'll figure things out and get better at Matlab slowly. Thank you for the help!

Iniciar sesión para comentar.

Más respuestas (2)

VBBV
VBBV el 15 de Ag. de 2020
Editada: VBBV el 15 de Ag. de 2020

1 voto

Use clear command as e.g
clear B W X Y Z
or
clearvars B W X Y Z in your script file or the function file in the end line
So, when you save the workspace variables as *.mat they will not appear in the *.mat file when you load it back into workspace

4 comentarios

Walter Roberson
Walter Roberson el 15 de Ag. de 2020
is there documentation for that? I do not find it. https://www.mathworks.com/help/matlab/ref/clear.html
VBBV
VBBV el 15 de Ag. de 2020
Agree with you Walter, there is no documentation for it
Sclay748
Sclay748 el 17 de Ag. de 2020
I tried this one, but it did not work for me. I am just trying to clean the variables so it wouldnt be confusing when someone loads it later.
VBBV
VBBV el 20 de Ag. de 2020
Editada: VBBV el 20 de Ag. de 2020
Try
delete B W X Y Z
At the end of file

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 15 de Ag. de 2020

1 voto

A work-around that is sometimes good enough is to create the variable locally with empty contents, and save with the -append flag. The old variable in the file will be marked as unused and the new empty variable will be saved in the file.
This does not completely remove the variable from the file: the variable name would still be there, just associated with empty content. But the previous content would no longer be associated with the name.

Categorías

Productos

Versión

R2020a

Preguntada:

el 14 de Ag. de 2020

Editada:

el 20 de Ag. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by