Alternative for eval of dynamic variables

조회 수: 4 (최근 30일)
Wout Depoot
Wout Depoot 2020년 4월 3일
댓글: Wout Depoot 2020년 4월 4일
Hello,
From what I understand from the online community is that the use of dynamic variables is not acceptable/accurate/buggy.
I have 11 meshgrids (xyz-grids; to make it easy e.g. A,B,...,K) laying next to each other. For each of these 11 grids I want to calculate changing volumes over the years using 17 datasets (= xyz-data through the years). (the meshgrids have different sizes, that's why I did not make an overall grid)
I already assigned the xyz-data to my grids with scatteredInterpolant, so right now I have 17 variables (98,00,...,17) containing the same xy-values but different z-values (through the years; in total 11x17=189 variables; e.g. A_98,A_00,...,A17,B_98,...,K_17).
Now I need to calculate the volumes (=(difference between z-value & reference level)*gridsize; e.g. 50x50 and ref level -20) for each dataset for each mesh grid (so 17x11 times); afterwards I'll compare the results to each other so no need to worry about that.
The reason I like to use dynamic variables is the fact that results can be interpreted (more?) easily and that I don't really understand the online solutions for this.
Is there an efficient way to make a function or adjust my method/grids/variables/names to calculate the volumes of at least all datasets of 1 meshgrid (so 17x times) or even a way this can be done for all meshgrids at once (so 17x11 times)? (In the past I wrote everything 11x17 times which was very inefficient/dangerous for mistakes)
Thanks in advance for reading this (& maybe even helping me)!
  댓글 수: 4
Stephen23
Stephen23 2020년 4월 4일
편집: Stephen23 2020년 4월 4일
"Is there an efficient way..."
No.
You designed your data badly, which means you have forced yourself into writing inefficient code.
Bad data design cannot be fixed by patching it up afterwards via super-efficient magic commands that do not exist.
"The reason I like to use dynamic variables is the fact that results can be interpreted (more?) easily..."
Indexing is so simple and easy to understand that millions of programmers use indexing every day in countless different programming languages. Not using indexing is like cutting your feet off before running a race.
"In the past I wrote everything 11x17 times which was very inefficient/dangerous for mistakes"
Computers are really only good at one thing: doing simple tasks repeatedly in a loop. So whenever you copy-and-paste code like that you are just doing the computer's menial job for it (as well as increasing the risk of bugs, making code pointlessly complex, harder to maintain, etc.). The computer can repeat the task very efficiently when you use indexing.
Wout Depoot
Wout Depoot 2020년 4월 4일
Thanks everyone, this helped a lot!

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2020년 4월 3일
If you use a cell array you can store everything in a single array even if the size are different. Because you can index into the cell array you can use a simple for loop, removing any need to repeat your code.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by