How to clear everything but a structure
Mostrar comentarios más antiguos
Hello everyone,
In my code I have a huge for loop that runs several times for different text files that contain data. Each time the code runs I am able to extract a bunch of information and in the end I put the information that I want in a structure. The name of the structure is dependent of the ID of the text file.
So here is my problem, a bunch of variables in my work space are not needed after the structure is build. So how to I clear everything but the structures?
I can't use 'clearvars -except' because the name of the structure is dependent in the text file ID and there is no specific pattern to follow.
I am hoping clearing things from my work space would allow my code to run faster and solve the out of memory error I get in matlab.
Thank You,
M
5 comentarios
KSSV
el 9 de Sept. de 2017
To comment ..we need to see your code...I also don't understand, what's the necessity to store every information from text file into workspace?
Mariam Salem
el 9 de Sept. de 2017
"The name of the structure is dependent of the ID of the text file"
Uh oh, that is a really bad way to write code ...
"I can't use 'clearvars -except' because the name of the structure is dependent in the text file ID and there is no specific pattern to follow"
Oh, what a surprise: you chose the buggiest, slowest, most horribly complex way to write code, and now you are finding it buggy, slow, and hard to work with.
If you wrote better code then you would not have this problem.
The solution is very simple: do not put meta-data into variable names.
I know, some beginners think that it is really cunning to store meta-data in variable names, and then they try to access their data... stored in five thousand numbered variables. Have you ever tried to pass five thousand numbered variables to a function? I haven't, and I wouldn't even bother trying: like all experienced MATLAB users I never put meta-data into variable names, and so never have to fix the pointless kind of problems that you are facing now.
Put the data into one array (it could be a non-scalar structure, a cell, a table, an ND array, etc), and use indexing. Indexing is simple, neat, very efficient, easy to debug, and will make your life much easier.
Read these to know why your approach is doomed to be slow, inefficient, buggy, complex, hard to debug, and a total waste of your time:
And read this to know why forcing meta-data (e.g. an index, or filenames, or user inputs) into variable names is a bad idea, even regardless of the problems associated with string evaluation:
You are already using a structure, so you should really just add a field for storing any meta-data:
S.fid = filenameID
Summary: do NOT do this:
eval([v '=testz']);
and do NOT put any meta-data into variable names. Ignore other beginners who tell you to use eval. You will only learn to write neater, simpler and much more efficient MATLAB code when you learn to use arrays, indexing, and fieldnames.
PS: clearing the workspace is something that most experienced users also rarely need to do, because they use functions rather than scripts. Scripts are useful for playing around, but not for any serious code that should be robust and reliable. Functions avoid needing to do a lot of inefficient runtime introspection (like clearing variables from memory).
José-Luis
el 9 de Sept. de 2017
"clear my workspace"
Does this mean that you are running a script instead of a function? You wouldn't have this problem if you used a function that returns only what you need. But that would just mask the problem anyway.
That being said you should follow Stephen's advice if you want to cut the problem from the source.
Mariam Salem
el 9 de Sept. de 2017
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Workspace Variables and MAT Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!