MATLAB: How to quickly index the first cell of sets of the array with near similar names

indexingMATLAB

Hey all, Here is my workspace:
I want to know if there is any way to index the first cell of each P_ arrays based on (Jan, Feb, Mar,…, Dec, Win, Spr, Sum, Aut, Annual)
For example in this picture I want :
8, 12, 5, 8, 9, 8 , 7, 5, 5, 6, 6, 7, 7, 5, 2, 3, 8
(January to annual)
Thank you all

Best Answer

  • You can do that using eval but it is not efficient to use eval in your code frequently
    mn={'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Win','Spr','Sum','Aut','Anuual'};
    x = zeros(1,length(mn));
    for i=1:length(mn)
    x(i) = eval(['P_' mn{i} '(1)']);
    end