Finding if the value is present in the table using readtable
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hi,
I have a taken an excel file under the readtable. I have chosen of the columns (say 5th ) and takien it as a vector. The column consits of address with 42 literals (I am representing IP address). My next step is that I am asking the user to input an address and check if the address is present in the column vector of more than 3000 datasets containing the 3000 addresses. My question is that if I input an address, how do I check if that address is present in the given column vector. For example
1) My table containing 3000 datasets is A= readtable('dataset1.xlsx');
2)from_node1 = A(:,5);
I am taking up the 5th column containing address like "0x0067f95a79c3c404a9d128168ddfdf3cb70c0852", "0xe7844a3cbc712652fb97f6170603fd7d4b1cd6f1" etc
3) I am asking the user to input an address say using : testnode=input("Enter the node address");
4) Say if I want to check if "0x0f9d959667be0fd0bd2917feba0aab7ac4ca9ce7" is present in the chosen column from the table A, the output should be '0'.
How do I code this in Matlab. Your help is much appreciated. Thanks for your time.
채택된 답변
Voss
2022년 6월 28일
Try this:
A = readtable('dataset1.xlsx');
% use 's' argument in input(), so that the user input
% is stored directly in testnode and not interpreted:
testnode = input("Enter the node address", 's');
output = ismember(testnode,A{:,5}); % use {} not ()
댓글 수: 11
Hi,
Thanks for the comment. I tried this way but the output ends the program. Hence I tried using if - else statement. The code is as follows:
if output ~= ismember(testnode,A{:,5})
disp("M");
elseif output == ismember(testnode,A{:,5})
disp("N");
end
What ever address I type in I get "N" as output. Even if the address is not in the table, I have this problem. If possible kindly help fixing this.
Thanks again
If you are defining the variable output as:
output = ismember(testnode,A{:,5}); % use {} not ()
and then comparing output to ismember(testnode,A{:,5}), then that comparison will always be true (and you'll get "N") because output is defined to be ismember(testnode,A{:,5}).
Maybe you mean to do this:
if ismember(testnode,A{:,5})
disp("N");
else
disp("M");
end
or maybe the "N" and "M" should be switched; I don't know what you want displayed in which situation.
Hey thanks. I have fixed it
You're welcome!
Padmapriya Sampathkumar
2022년 7월 9일
편집: Padmapriya Sampathkumar
2022년 7월 9일
Hi Voss,
I have one more question. As said I have the node address as inputs to my model. I want to check if the 3rd literal of the address is of interger or character. Depending upon that I would classify as M or N. Here is the code, I keep getting wrong results as it reads even the numbers as strings.
testdata = input("Enter the node to validate","s");
a=testdata(3); // Reads the 3rd literal
if isa(a,'int64')
disp("M");
else
disp("N");
end
The curly braces throws error stating it isn't valid. Kindly help me out. Thanks.
I don't see any curly braces.
Sorry, I tried different options and that's why mentioned about the same. Is there a way to check if the literal is chat Or number?
testdata will be a char, because of the "s" used in input:
testdata = input("Enter the node to validate", "s" );
Therefore, testdata(3) will be the 3rd character of testdata, e.g.,
testdata = 'here is a test';
testdata(3)
ans = 'r'
It sounds like you want to parse words from a character vector (e.g., testdata = 'number is 629') and determine whether the 3rd word ('629') is a representation of a number. Is that right? If so, you need some code to parse the character vector into "words", (e.g, words = split(testdata,' '))
testdata = 'number is 629'
testdata = 'number is 629'
words = split(testdata,' ')
words = 3×1 cell array
{'number'}
{'is' }
{'629' }
% check whether each word represents a number
~isnan(str2double(words))
ans = 3×1 logical array
0
0
1
% another way that might work
cellfun(@all,isstrprop(words,'digit'))
ans = 3×1 logical array
0
0
1
Thank you for the reply. Say if the value of Testnode='0xc3336..... 7' ( has 42 literals. I need to analyse if 3rdliteral is a char or integer. Here in this case it is 'c'. Depending upon that I would make a classification. I tried with isa, ischar(A) etc. I am not getting the required result. I will try with your suggestion and get back to you.
testnode(3) is a single character, always.
Here's one way to make a decision based on whether that character is a digit ('0' - '9') or a letter ('a' - 'f'):
testnode='0xc3336'; % letter
ismember(testnode(3),'0':'9')
ans = logical
0
ismember(testnode(3),'a':'f')
ans = logical
1
testnode='0x43336'; % digit
ismember(testnode(3),'0':'9')
ans = logical
1
ismember(testnode(3),'a':'f')
ans = logical
0
Another way:
testnode='0xc3336'; % letter
isstrprop(testnode(3),'digit')
ans = logical
0
isstrprop(testnode(3),'alpha')
ans = logical
1
testnode='0x43336'; % digit
isstrprop(testnode(3),'digit')
ans = logical
1
isstrprop(testnode(3),'alpha')
ans = logical
0
Hey thanks. I tried the first option. I had the same logic but misssed the string notation as '0':'9'. I wrote 0:9. Thats why I didnot get the results. Thanks again for your time.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
참고 항목
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)
