Azzera filtri
Azzera filtri

Searching values in a dataset

8 visualizzazioni (ultimi 30 giorni)
Mark
Mark il 27 Feb 2012
Hi
I need to apply some filters to a dataset, or just look for some specific quantity. However, I receive an error that the eq operator is not defined for datasets. I know I can convert a particular column to, e.g., double and then use the eq operator for double, but if the dataset is big, I am concerned about performance overheads due to constant conversions.
So
  • Is there any way to apply a filter to a dataset without converting it first to other type (e.g., double)?
  • Is there any way to look for values in specific columns of a dataset without converting such column first to other type (e.g., double)?
Thanks in advance

Risposta accettata

Peter Perkins
Peter Perkins il 27 Feb 2012
Quite often, this is the kind of thing you'd do to pick out rows of a dataset array:
>> load hospital
>> hospital(hospital.Age>45 & hospital.Sex=='Male',{'LastName' 'Age' 'Smoker'})
ans =
LastName Age Smoker
KOQ-996 'MARTIN' 48 true
XBA-581 'ROBINSON' 50 false
YYV-570 'SCOTT' 47 false
FLJ-908 'STEWART' 49 true
DAU-529 'REED' 50 true
FCD-425 'GONZALES' 48 false
MEZ-469 'GRIFFIN' 49 false
ZZB-405 'HAYES' 48 false
There's not really any need to explicitly "convert" Age to double, just by subscripting using dot, you get back the Age variable itself. Similarly for the Sex variable. Performance wise, this is actually (under the hood) a shared copy of something that already exists, so it should be fast.
  3 Commenti
Mark
Mark il 27 Feb 2012
I found the answer myself, but with your valuable help of course:
>> strField='Sex';
>> strValue='Male';
>> hospital( hospital.(strField) ==strValue,{'LastName' 'Age' 'Smoker'})
I didn't know about the syntax .(field name)
Peter Perkins
Peter Perkins il 28 Feb 2012
Yes, just as with a structure array, you can use that "dot parentheses" syntax if the name is stored in a string variable.
Your initial guess, which involved
hospital(:, {strField})==strValue
illustrates a point that is sometimes hard to understand at first: a dataset array (much like a cell array) is a container, and subscripting using parentheses preserves that "containerness", and so hospital(:, {strField}) is dataset array that only contains one variable, but it is not the same thing as that variable. Much the same as with a scalar structure, it's the dot subscripting that gets you the "contents".
One more minor point: hospital(:, strField) (without the braces around strField) would have been fine, since you've only got one variable name.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by