How to avoid using dynamic variables?

3 views (last 30 days)
Lee
Lee on 5 Sep 2018
Answered: dpb on 5 Sep 2018
I'm a beginner and recently found out that I was writing code inefficiently. I have been making dynamic variables and copying/pasting code while changing minor details when I could be using structures or arrays. I tried reading more into how to make my code more efficient but am still confused. Can someone help me better understand how to avoid using dynamic variables? I learn best through examples.
Here's an example of what I have been doing:
I have 3 tables (A,B,C), each with 5 columns (SampleName, pH, Temp, WindDir, WindSp).
The sample name has indexes that separate the samples into groups. So I have been doing this:
Samples1A = A(startsWith(A.SampleName,'1C',:);
Samples2A = A(startsWith(A.SampleName,'2C',:);
...
Samples10A = A(startsWith(A.SampleName,'10C',:);
(Copy and paste this group of code and repeat for table B and table C)
I then try to average each column (pH, temp, windir, windsp) and group them acccordingly:
Grouped_pH = [nanmean(Samples1A.pH),nanmean(Samples1B.pH),nanmean(Samples1C.pH);...
nanmean(Samples2A.pH),nanmean(Samples2B.pH),nanmean(Samples2C.pH);...
...
nanmean(Samples10A.pH),nanmean(samples10B.pH),nanmean(Samples10C.pH)]
(I repeat this for each column).
I'm then able to use "Grouped_pH" or "Grouped_Temp", etc. to make a bar graph.
How do I get the same output (Grouped_pH) but without having to copy and paste so much code?

Answers (1)

dpb
dpb on 5 Sep 2018
Good on ya' to recognize the futility of the approach! :)
See
doc splitapply
for the general utility function to apply a function to groups of data within a table.
doc findgroups
will let you get the group ids and use it independently as well.
One key point; in a table such as you have, the SampleName variable should be of the categorical type for efficiency; makes lookup and testing much simpler than with string variables. Also, if the variable name is such that there are actually two variables combined (that is, the '1' and the 'C' may both have different values, then you should separate those into two variables so that can group over either the letter mnemonic or the numeric value. If they're all always 'nC' where the 'C' is invariant, then it doesn't matter.
After that, instead of creating three separate tables A, B, C, either merge those into one table with another variable that is the identifier if need to keep them separate or just read the data in one-at-a-time if all analyses are within a single table rather than across tables, and use a generic name for the table.

Categories

Find more on Tables 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!