How to group variables together

31 vues (au cours des 30 derniers jours)
George Head
George Head le 7 Jan 2020
Modifié(e) : Stephen23 le 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 commentaire
Stephen23
Stephen23 le 7 Jan 2020
Modifié(e) : Stephen23 le 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.

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 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.

Plus de réponses (1)

dpb
dpb le 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.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by