how to create table Date/Value

i have this data (pic1) and i want similar pic2
Do I have to create the table by entering the values manually or is there a function to do this?

1 Commento

Unless you provide specific and necessary details, in written form, explaining what the expected output is, I will be closing the question.

Accedi per commentare.

 Risposta accettata

The unstack function works here.
Try this —
LD = load('matlab_data.mat')
LD = struct with fields:
prof: [728×1 double] dat: [728×1 datetime]
T1 = table(year(LD.dat),month(LD.dat,'shortname'),LD.prof)
T1 = 728×3 table
Var1 Var2 Var3 ____ _______ _______ 2022 {'Jan'} 0 2022 {'Jan'} 200.25 2022 {'Jan'} 346.25 2022 {'Jan'} -384.75 2022 {'Jan'} -171.5 2022 {'Jan'} 1026.2 2022 {'Jan'} 30.5 2022 {'Jan'} 0 2022 {'Jan'} -64.5 2022 {'Jan'} 39 2022 {'Jan'} 1695.8 2022 {'Jan'} 278.5 2022 {'Jan'} 12.5 2022 {'Jan'} 146.25 2022 {'Jan'} 0 2022 {'Jan'} -62.75
ResultSum = unstack(T1, 'Var3', 'Var2');
ResultSum.Properties.VariableNames{1} = 'Year'
Result = 2×13 table
Year Apr Aug Dec Feb Jan Jul Jun Mar May Nov Oct Sep ____ _______ _______ _______ _______ ______ ______ _______ _____ ______ _____ ______ _______ 2022 -2089.8 2563.8 -802.25 2864.8 1704.6 8589.7 -76.6 10816 8181.1 207.5 2008 614.85 2023 -5200.1 -657.25 1442.1 -355.12 5467.4 -227.2 -2292.4 6561 2739.1 3667 3315.6 -4046.3
The unstack function sums the data by default for each month. Other options are available by defining the appropriate function.
Example —
ResultMean = unstack(T1, 'Var3', 'Var2', 'AggregationFunction',@mean);
ResultMean.Properties.VariableNames{1} = 'Year'
Result = 2×13 table
Year Apr Aug Dec Feb Jan Jul Jun Mar May Nov Oct Sep ____ _______ _______ _______ _______ ______ ______ _______ ______ ______ ______ ______ _______ 2022 -69.658 82.703 -25.879 102.31 54.987 277.09 -2.5533 348.9 263.91 6.9167 64.774 20.495 2023 -173.34 -21.202 49.726 -12.683 176.37 -7.329 -76.412 211.65 88.357 122.23 106.95 -134.88
.

6 Commenti

thank you
As always, my pleasure!
Result = 2×13 table
Year Apr Aug Dec Feb Jan Jul Jun Mar May Nov Oct Sep
____ _______ _______ _______ _______ ______ ______ _______ ______ ______ ______ ______ _______
2022 -69.658 82.703 -25.879 102.31 54.987 277.09 -2.5533 348.9 263.91 6.9167 64.774 20.495
2023 -173.34 -21.202 49.726 -12.683 176.37 -7.329 -76.412 211.65 88.357 122.23 106.95 -134.88
it's possible to have it in this order :
Year Gen feb Mar .. Nov Dec
I did not check on the month name orders. They are sorted alphabetically, not chronologically. (I do not see options for sorting them differently, so this requires a slightly indirect approach.)
To change that, it is necessary to initially use the month numbers, then over-write the variable names with the appropriate month names.
Try this —
LD = load('matlab_data.mat');
T1 = table(year(LD.dat),month(LD.dat),LD.prof)
T1 = 728×3 table
Var1 Var2 Var3 ____ ____ _______ 2022 1 0 2022 1 200.25 2022 1 346.25 2022 1 -384.75 2022 1 -171.5 2022 1 1026.2 2022 1 30.5 2022 1 0 2022 1 -64.5 2022 1 39 2022 1 1695.8 2022 1 278.5 2022 1 12.5 2022 1 146.25 2022 1 0 2022 1 -62.75
ResultMean = unstack(T1, 'Var3', 'Var2', 'AggregationFunction',@mean)
Warning: Table variable names that were not valid MATLAB identifiers have been modified. Since table variable names must be unique, any table variable names that happened to match the new identifiers also have been modified.
To use the original INDVAR values as table variable names, set 'VariableNamingRule' to 'preserve'.
ResultMean = 2×13 table
Var1 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 ____ ______ _______ ______ _______ ______ _______ ______ _______ _______ ______ ______ _______ 2022 54.987 102.31 348.9 -69.658 263.91 -2.5533 277.09 82.703 20.495 64.774 6.9167 -25.879 2023 176.37 -12.683 211.65 -173.34 88.357 -76.412 -7.329 -21.202 -134.88 106.95 122.23 49.726
ResultMean.Properties.VariableNames{1} = 'Year';
monthnames = month(datetime(2024,1,1)+calmonths(0:11), 'shortname');
ResultMean.Properties.VariableNames(2:end) = monthnames
ResultMean = 2×13 table
Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ____ ______ _______ ______ _______ ______ _______ ______ _______ _______ ______ ______ _______ 2022 54.987 102.31 348.9 -69.658 263.91 -2.5533 277.09 82.703 20.495 64.774 6.9167 -25.879 2023 176.37 -12.683 211.65 -173.34 88.357 -76.412 -7.329 -21.202 -134.88 106.95 122.23 49.726
Extracting the numbers after the ‘x’ values in ‘x1’...‘x12’ and then assigning the appropriate month names to them is possible, however it is just easier to initially let unstack sort them numerically and then create the month names separately and use those as the variable names. (I checked with the original alphabetically-sorted table, and the new version assigns evertyhing correctly.)
.
👍👍
Thank you!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by