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.

 Accepted Answer

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 Comments

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.
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!