Unrecognized table row name

조회 수: 13 (최근 30일)
LeoAiE
LeoAiE 2021년 9월 6일
댓글: LeoAiE 2021년 9월 7일
Hello everyone,
I have a table and I want to subscript using a column contain a string array. Basically, the column is repeated countries names and I used unique to get unique values. Then I wanted to get all the available information from the table about those countries. I understand I will lose some data from other columns, but this is not a concern at this time. I get an error message saying “unrecognized row name”
Any other methods can achieve the results? here is my code and my data
Data = readtable('Test.xlsx');
Data.country = string(Data.country);
Data(unique(Data.country),:)

채택된 답변

Walter Roberson
Walter Roberson 2021년 9월 7일
In order to index by a string or categorical in the first index of a table, the values have to have been set as the RowNames property.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/730924/Test.xlsx';
Data = readtable(filename);
Data.Properties.RowNames = string(Data.country);
Duplicate table row name: 'Andorra'.
Data(unique(Data.country),:)
You can see from this that every row name must be unique.
The indexing done with the first index of a table is not equivalent to a SELECT operator that would select all matches. Instead, the RowName that is set must be unique, must identify exactly one row in the table.
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 9월 7일
If you want to get at the first match for each country:
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/730924/Test.xlsx';
Data = readtable(filename);
Data.country = string(Data.country);
[~, ia] = unique(Data.country);
Data(ia,:)
ans = 19×4 table
lat lon country RandomCode _______ _________ _____________________ __________ 33.939 67.71 "Afghanistan" 41 41.153 20.168 "Albania" 53 -14.271 -170.13 "American Samoa" 59 42.546 1.6016 "Andorra" 1 -11.203 17.874 "Angola" 95 18.221 -63.069 "Anguilla" 54 -75.251 -0.071389 "Antarctica" 24 17.061 -61.796 "Antigua and Barbuda" 79 -38.416 -63.617 "Argentina" 83 40.069 45.038 "Armenia" 53 12.521 -69.968 "Aruba" 99 -25.274 133.78 "Australia" 58 47.516 14.55 "Austria" 64 40.143 47.577 "Azerbaijan" 83 23.685 90.356 "Bangladesh" 95 13.194 -59.543 "Barbados" 46
LeoAiE
LeoAiE 2021년 9월 7일
@Walter Roberson Thank you so much I really appreciate your help. I always enjoy reading detailed explanations when you answer my questions and have a full understanding of where I missed up and how to fix it! You are awesome man. Keep up the great work.

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by