Finding if the value is present in the table using readtable
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
Voss
on 28 Jun 2022
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
Padmapriya Sampathkumar
on 29 Jun 2022
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
Voss
on 2 Jul 2022
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.
Padmapriya Sampathkumar
on 3 Jul 2022
Hey thanks. I have fixed it
Voss
on 3 Jul 2022
You're welcome!
Padmapriya Sampathkumar
on 9 Jul 2022
Edited: Padmapriya Sampathkumar
on 9 Jul 2022
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.
Voss
on 10 Jul 2022
I don't see any curly braces.
Padmapriya Sampathkumar
on 10 Jul 2022
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?
Voss
on 10 Jul 2022
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
Padmapriya Sampathkumar
on 10 Jul 2022
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.
Voss
on 10 Jul 2022
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
Padmapriya Sampathkumar
on 10 Jul 2022
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.
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
See Also
on 28 Jun 2022
on 10 Jul 2022
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)