MATLAB: Time series, months and sequence

monthsequencetime seriesyear

Hi everyone,
I have 12 series:
x1 = [1990M1, 1991M1, 1992M1 …..2020M1]
x2=[1990M2, 1991M2, 1992M2 …2020M2]
x12=[1990M12, 1991M12, 1992M12 …2020M12]
Now I would like a time sereis:
x= [1990M1, 1990M2, 1990M3 …2020M12].
Please give me a hint.
Many thanks,
Bao

Best Answer

First, you should avoid creating variables names like x1, x2, ..., xn. This thread explains the issues with this approach in detail: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Now, if you want to correct this issue by combining all these values in a single array, then you can use eval(). Note that using frequently using eval() in your code is not a very good idea and should be avoided, but you can use it once to fix the initial mistakes. This code shows an example.
x1 = [1 2 3];
x2 = [4 5 6];
x3 = [7 8 9];
x4 = [10 11 12];
x5 = [13 14 15];
x = [];
for i=1:5 % numbers of the x variables
eval(['x = [x; x' num2str(i) '];']);
end
x = x(:)';
Related Question