how to create table Date/Value
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
Dyuman Joshi
2024년 1월 7일
Unless you provide specific and necessary details, in written form, explaining what the expected output is, I will be closing the question.
채택된 답변
Star Strider
2024년 1월 7일
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
shamal
2024년 1월 7일
thank you
Star Strider
2024년 1월 7일
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'.
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.)
.
shamal
2024년 1월 7일
👍👍
Star Strider
2024년 1월 7일
Thank you!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
