How to clear everything but a structure

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 Comments

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?
My code is huge but what I use to build the structure is this:
v=matfiles(i,1).name;
testz=struct ('ID', ID{1,i}(1), 'FlagsTable',TFlags{1,i},'RC', RAINCORRECTED{1,i},'Start',YEAR{1,i}(1),'End', YEAR{1,i}(length(YEAR{1,i}))}
eval([v '=testz']);
I am not storing every information from the text file into space but there are some calculations that I do and the results end in my work space...After getting the end results and putting then in a structure so values become unneeded and I could use the space.
I hope this makes things clearer
"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).
"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.
Stephen Cobeldick Thank you for your comment and help. I am a beginner at matlab that's why I post questions here in hopes to get help and be better at coding from this great community.

Sign in to comment.

 Accepted Answer

OCDER
OCDER on 9 Sep 2017
Edited: OCDER on 9 Sep 2017
Try wrapping your script for extracting Data from a File into a function called getFileData.
function Data = getFileData(File)
%Temporary variables can be created here.
Data = ......; %Do something to get Data from your File.
end %Temporary variables are cleared automatically!
Then in your script, fill up your structure S with data.
FileNames = {'file1.txt', 'file2.txt'};
S(1:length(FileNames)) = struct('Data', []); %Preallocate
for j = 1:length(FileNames)
S(j).Data = getFileData(FileNames{j});
end
The only variables in the workspace will be S and FileNames.
Use clear or clearvars rarely because it's hard to track variables you want to keep/remove when modifying codes, and you could accidentally delete variables.
If you want to access the jth data, use S(j).Data, which is MUCH BETTER than something like S.(['File' num2str(j)]).

2 Comments

Thank You^^ This is very helpful!!
"Use clear or clearvars rarely because it's hard to track variables..."
They are also very slow.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 9 Sep 2017

Commented:

on 10 Sep 2017

Community Treasure Hunt

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

Start Hunting!