How to group variables together

38 views (last 30 days)
George Head
George Head on 7 Jan 2020
Edited: Stephen23 on 7 Jan 2020
I have many variables and they are all related to one another, and then many variables that are seperate.
I was wondering if there is a way to create like a sub folder of variables within the matlab workspace? so that certain variables are easier to find.
for example i have variables called A1z, A1o, A1t, A1T and would like these to all be contained within a type of 'folder' i suppose.
Thanks
  1 Comment
Stephen23
Stephen23 on 7 Jan 2020
Edited: Stephen23 on 7 Jan 2020
"I have many variables..."
Uh oh... that sounds like the start of a question caused by badly designed code/data.
"...they are all related to one another..."
The sensible way to handle "related data" would be to put them all into container arrays (e.g. cell array, structure array, table, etc.) or numeric arrays if possible.
"I was wondering if there is a way to create like a sub folder of variables within the matlab workspace? so that certain variables are easier to find."
Yes: they are called "arrays": container arrays if you have to or numeric arrays if possible. Using arrays is the key to writing neat and efficient code without thousands of badly named variables polluting your workspace. Using arrays like that makes data easy to "find" because it never gets lost in the first place. It also makes processing your data simple and efficient using loops or vectorized code.
"for example i have variables called A1z, A1o, A1t, A1T..."
Then you have badly designed data and should be using arrays. Your variable names seem to contain meta-data, and that is a sign that you are are doing something wrong with your code/data design: the simple and efficient alternative is to use arrays.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 7 Jan 2020
You probably want to use a struct array or perhaps a table array. If your data is date and/or time based, a timetable array will store your data in a tabular form while providing some time-based manipulation capabilities.

More Answers (1)

dpb
dpb on 7 Jan 2020
Not as "folders" per se, no. MATLAB doesn't have the concept of a scoping mechanism for variables independently.
Where you seemingly went wrong is in using variable names to incorporate meta data instead of arrays or other higher-level structures. Then all A (or at worst, A1) variables would be in one and reduce the clutter dramatically.
Plus, then one can write generic code to process all similar variables instead of having to have explicit code for every individual one.

Categories

Find more on Dates and Time 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!