How do I create multiple bar graphs in one using a for loop and eval?

3 views (last 30 days)
The assignment I have is as follows:
Given the structure Group1 below use eval in the for loop to assign Group1.Function1 Magnitude to x(1,:) and Group1.Function2 Magnitude to x(2,:) and Group1.Function3 Magnitude to x(3,:). Plot three bar graphs in one figure include title identifying Group1.Function1.Data.Magnitude on graph 1, Group1.Function2.Data.Magnitude on graph 2, and Group1.Function3.Data.Magnitude on graph 3,:
Group1.Function1.Data.Frequency = linspace(1,10,10)
Group1.Function1.Data.Magnitude = [1 2 2 2 7 6 1 10 8 8]
Group1.Function2.Data.Frequency = linspace(1,10,10)
Group1.Function2.Data.Magnitude = [4 94 31 30 34 47 65 3 85 56]
Group1.Function3.Data.Frequency = linspace(1,10,10)
Group1.Function3.Data.Magnitude = [855 348 447 55 178 663 331 899 119 989]
The problem is I have no idea where even to begin. I understand how to make bar graphs. But I have no idea how to incorporate it with eval and a for loop. My professor is not very proficient at teaching matlab. Thanks!
  1 Comment
dpb
dpb on 13 Apr 2018
Edited: dpb on 13 Apr 2018
"Not very proficent" is being more than kind.
Your professor should be banned from teaching Matlab by instructing using eval
Use the facility to generate structure field names from variables instead <generate-field-names-from-variables>
While you may need to do the other to receive credit for the course, forget you ever heard of eval and I'd recommend pointing your so-called "professor" to <Avoid Eval (like the plague)> and complain about the quality of instruction to your department head; this is criminal.
To do it in eval you have to build text that is the full text of the command to be executed just as if you were to type it at the command line then pass that string to eval. Building the one variably-named field name is one thing (and is the same for that part) but to build the full command is simply evil and wrongheaded in the worst possible way.
ADDENDUM
And, no, other than that, I have no opinion... :)

Sign in to comment.

Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!