Select value from the list by defining criteria

Dear All,
I have a big dataset with the format,
1 2019-09-25 19:33:00 27.7100 86.3200 10 291.122 4.00 1.79 R023E
1 2019-09-25 19:33:00 27.7100 86.3200 10 197.274 4.00 4.44 S8618
1 2019-09-25 19:33:00 27.7100 86.3200 10 170.200 4.00 4.14 RA5AB
I would like to have new set of data
(i) If column 10 has always valule R023E
(ii) If column 7 > 30.00
what is the solution?

 Accepted Answer

I would use readtable to import the data, the logic indexing to narrow down the results.
data = readtable('mydata.xlsx');
reducedata = data(data(:,10) == 'R023E' & data(:,7) > 30,:);

7 Comments

Hello Bob Nbob,
1 '2019-09-25' '19:33:00' 27.71 86.32 197.27 4 4.44 'S8618' 'Dolakha'
Thanks but there is error
Undefined operator '>' for input arguments of type 'table'.
reducedata = data(data(:,9) == 'R023E' & data(:,6) >30,:);
Mmm, replace 'data(:,6)' with 'data{:,6}'.
Alternatively you can also use 'data.*variable name*' if you know what the consistent variable name is.
I tried a sample using your conditions:
data = randi(100,20,1);
data = array2table(data); % Create a table of random numbers up to 100.
reducedata = data(data{:,1}>10^1.5 & data{:,1}<10^1.9,:);
This produces a result, which makes me think that maybe there's something wrong with the logic you're trying to check. Are you sure that your data set contains the values you're looking for? I wouldn't be able to check it without having a sample of your dataset.
Shiba Subedi's answer moved here as a comment. Please use the comment section for discussion and the answer section to propose answers.
191002 094435-Figure 2.png
Use dot indexing when working with columns from a table, not subscript indexing in curly brackets.
Could you share the result of the line below?
head(data) % data is the output from readtable
I would agree that dot indexing is more efficient, but I assumed that variable names were inconsistent or unknown.
That's a good point. However, the variable names can be set in readtable() or after the table is created. If it is known that the data in column #7 is needed, they can name that column anything they want.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!