Reorganise table by label name

조회 수: 1 (최근 30일)
Mario Diaco
Mario Diaco 2020년 4월 12일
편집: Mario Diaco 2020년 4월 12일
Hi everyone,
I've multiple tables imported from excel where they are organized one for sheet. They include observation for countries at different times (sheet1=1-st year, sheet2 = 2-nd year,...). I'm trying to reorder them by their table labels (country name).
Can you help me? Thanks.

답변 (1개)

Image Analyst
Image Analyst 2020년 4월 12일
Here is how to use sortrows() to reorder a table by the country names column:
% Sort table alphanumerically by the first column (the table with the country rows in it).
t = sortrows(t, 1)
Here is a full demo, which includes creating some data and then sorting it:
% No data was included in the question, so we are forced to create some on our own.
for row = 1 : 15
countries{row} = sprintf('Country %02d', randi(99, 1, 1));
data(row) = rand(1);
end
% Make a table from these two row vectors. Use (:) to turn them into column vectors, then use table().
t = table(countries(:), data(:), 'VariableNames', {'Country', 'Data'})
% Now we have the sample data with country names in random order, and we can begin.
% Sort table alphanumerically by the first column (the table with the country rows in it).
t = sortrows(t, 1)
  댓글 수: 3
Image Analyst
Image Analyst 2020년 4월 12일
Well, unless you can upload these multiple tables, or workbook with multiple sheets in it (ideally with code to read in all the sheets), you're making a lot harder for me. I think I'll just wait for the data, along with what you want as the output.
Mario Diaco
Mario Diaco 2020년 4월 12일
편집: Mario Diaco 2020년 4월 12일
Thanks, I think that I've solved my problem.
for c=1:width(CDS_1Y)
SpreadCurves{:,c}=[CDS_1Y(t,c);CDS_2Y(t,c);CDS_3Y(t,c);CDS_4Y(t,c);CDS_5Y(t,c);CDS_7Y(t,c);CDS_10Y(t,c);CDS_20Y(t,c);CDS_30Y(t,c)];
end
% "c" is the label (country) founder "t" is another parameter. "CDS_*" are originally tables names.
%% With this code you can pick the column for the same label from multiple tables and recombine these in a table for any label.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by