필터 지우기
필터 지우기

How to group variables together

조회 수: 19 (최근 30일)
George Head
George Head 2020년 1월 7일
편집: Stephen23 2020년 1월 7일
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