How to group variables together

28 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2020-1-7
编辑:Stephen23 2020-1-7
"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.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2020-1-7
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.

更多回答(1 个)

dpb
dpb 2020-1-7
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.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by