Hello,
I have a table of annual quarters as row names and the dates those quarters began as a variable. I'm trying to create a command where given a quarter (R) I can lookup the quarter start date. I've read the documentation and searched for answers here, but am very lost so I'd appreciate any help!
Quarter = {'Q12019'; 'Q22019'; 'Q32019'; 'Q42019'; 'Q12020'; 'Q22020'; 'Q32020'; 'Q42020'};
QuarterStartDate = ['2019-01-01'; '2019-04-01'; '2019-07-01'; '2019-10-01'; '2020-01-01'; '2020-04-01'; '2020-07-01'; '2020-10-01'];
T = table(QuarterStartDate, 'RowNames', Quarter);
R = 'Q32019';
S = T(Quarter == 'R')
I was able to do something similar earlier that worked, but that was using numbers rather than characters. I tried doing the same thing, but that didn't work.
C = [ .80 ; .85 ; .90 ; .95];
P = [.55 ; .49 ; .44 ; .44];
T = table(C, P);
R = 0.95;
S = T{C == R, 2};
Thanks!

5 件のコメント

dpb
dpb 2019 年 11 月 29 日
Help us out by attaching a (smallish) section of the table as .mat file...
Ivan Glinski
Ivan Glinski 2019 年 11 月 29 日
Just attached to my post! I just ran the first three lines of the code I shared.
Walter Roberson
Walter Roberson 2019 年 11 月 30 日
S = T(strcmp(T.Quarter,'R').QuarterStartDate;
Ivan Glinski
Ivan Glinski 2019 年 11 月 30 日
編集済み: Ivan Glinski 2019 年 11 月 30 日
Thanks for your help!
After adding a parenthesis at the end to close the command around T, I get an error:
"Unrecognized variable name 'Quarter'."
Quarter is the row name, not a variable. If I change it so that Quarter is a variable in the table, I get the following error:
"Error: Functions cannot be indexed using {} or . indexing."
Image Analyst
Image Analyst 2019 年 11 月 30 日
Try T.QuarterStartDate instead of T.Quarter.

サインインしてコメントする。

 採用された回答

Stephan
Stephan 2019 年 11 月 30 日
編集済み: Stephan 2019 年 11 月 30 日

1 投票

Quarter = {'Q12019'; 'Q22019'; 'Q32019'; 'Q42019'; 'Q12020'; 'Q22020'; 'Q32020'; 'Q42020'};
QuarterStartDate = ['2019-01-01'; '2019-04-01'; '2019-07-01'; '2019-10-01'; '2020-01-01'; '2020-04-01'; '2020-07-01'; '2020-10-01'];
T = table(QuarterStartDate, 'RowNames', Quarter);
R = 'Q32019';
S = T(R,:)
results in:
S =
table
QuarterStartDate
________________
Q32019 2019-07-01
if you need only the content use curly braces:
S = T{R,:}
S =
'2019-07-01'

1 件のコメント

Ivan Glinski
Ivan Glinski 2019 年 11 月 30 日
It works, thank you!! I sort of can't believe the solution was this simple, but I'm still learning to think in Matlab terms.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeTime Series Objects についてさらに検索

製品

リリース

R2018b

タグ

質問済み:

2019 年 11 月 29 日

コメント済み:

2019 年 11 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by