0

I have a cell array of variable names that I want to plot. For example,

var = {...
       'Xp_1';
       'Xp_2';
       'Xp_3';
       'PRES_1';
       'PRES_2';
       'PRES_3';
       'FLOW_1';
       'FLOW_2';
       'FLOW_3'};

Now, I want to group them by their names, that is, all Xps, PRESs, and FLOWs, and over-plot them on separate figure; thus, the total number of three figures: one for Xps, one for PRESs, and one for FLOWs.

I was brainstorming to do so by comparing the first two or three characters of all the variables.

How can I do this?

Eric
  • 409
  • 2
  • 15
  • How did you end up with all those variables? This is called "Using dynamic variable names" and that is [bad](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170) – Adriaan Oct 26 '15 at 17:00

1 Answers1

0

You can extract a substring and compare the results via logical operation or you can use this method.

figure(1); hold all; figure(2); hold all; figure(3); hold all;
for i = 1:length(var)
 if strncmpi('xp',var(i),2) // compare the first 2 characters
      figure(1)
       plot(eval(var(i))) // eval calls the variable by the name in var(i)
 elseif strncmpi('pr',var(i),2)
      figure(2)
       plot(eval(var(i)))
 elseif strncmpi('fl',var(i),2)
         figure(3)
       plot(eval(var(i)))
 else
  disp('No case for variable name');
end
peng
  • 300
  • 1
  • 12
  • 1
    Can you provide an example? This isn't enough to answer the question. There's a plotting element involved with this question too. – rayryeng Oct 26 '15 at 17:02
  • It's a simulation result with a model that has hundreds of parameter names. I bring the simulation data as a struct and I do 'fieldnames' on it to get a cell array of variable names and 'struct2cell' to get a cell array of their time histories. Then I '[ ]' them (let's call it 'data'). I do then plot(time_vector, data{i,2});ylabel(data{i,1}) in a while loop (time_vector is also extracted from data). – Eric Oct 26 '15 at 19:36