Search code examples
arraysmatlabcell

MATLAB: Looping Through a Cell Array


In MATLAB, I have a csv file with the data (ignore whitespaces below only for readable):

State,  Rain, Sunshine,
Indiana,  52,    25,
Kansas,   45,    22,
Georgia   35,    55,
Texas     22,    30,
Arizona   60,    12,

I need to create a for loop that will step through the cell array State:

   State = {'Indiana','Kansas','Georgia','Texas','Arizona'}
   for n = 1:numel(states)

and generate a separate plot for Rain vs. Sunshine for each state.

Thanks,

Amanda


Solution

  • I'm not sure exactly what you require, but this will plot the Rain and Sunshine of each state.

    Rain = [52,45,35,22,60];
    Sunshine = [25,22,55,30,12];
    State = {'Indiana','Kansas','Georgia','Texas','Arizona'};
    
    figure
    hold on
    for n = 1:numel(State)
    plot(Rain(n),Sunshine(n),'.')
    text(Rain(n),Sunshine(n),State(n))
    end
    xlabel('Rain')
    ylabel('Sunshine')
    

    With this result:

    Plot of rain v sunshine