Select data by attributes

Hi
I have a 24000x1 Geographic Data Structure (let's call it "Polylines" with more than 20 attribute fields. I always only like to do calculations with some of the data of this stucture. Let's say I have the field "Accuracy" and now I only like to do calculation with the data which have the string "acc" in that field. I am not sure if it is important but the structure is an element-by-element-organization. How can I select these data? Thank you for your help, Claudia

 Accepted Answer

Do you mean you have a structure:
Polylines.Accuracy
Polylines.SomeOtherField1
Polylines.SomeOtherField2
... etc
And the Polylines.Accuracy field is exactly equal to the string 'acc' for all the rows you're interested in?
And the Polylines.Accuracy field is perhaps a cell array?
IF that were the case, you could do this:
validRows = strcmp( Polylines.Accuracy, 'acc' );
And then use that as a logical index on all fields you are interested in.

8 Comments

Hi Geoff
thanks for your answer.
With Polylines.ACCURACY (case sensitive) I get all the values for this field.
validRows = strcmp( Polylines.ACCURACY, 'acc' ) does not work:
Error using strcmp
Too many input arguments.
Maybe it is because the data are a struct and not a cell.
My struct looks like:
Polylines <24000x1 struct>
Each of the 24000 cells look as follows <1x1 struct>
Then
Polylines(5,1) <1x1 struct>
with many attributes fields, one of them Polylines(5,1).ACCURACY
Then
Polylines(5,1).ACCURACY <1x3 char>
for this specific one the entry is acc
I tried
validRows = strcmp( [Polylines.ACCURACY], 'accurate' ) and got the answer
validRows = 0
So, doesn't work neither.
Do you have another idea how to deal with the struct?
Thanks, Claudia
Geoff
Geoff on 21 May 2012
Ah right, in that case you can use the following syntax to condense the multiple answers into a cell array.
{ Polylines.ACCURACY }
Hi Geoff
Thanks for your prompt answer. I am really new to Matlab and I miss the last step (and a informative book or other resource about struct in MATLAB in the case you know something you could recommend)
I did:
command: ACCURACY = {Polylines.ACCURACY};
result: ACCURACY <1x24000 cell>
command: validRows = strcmp(ACCURACY, 'acc');
result: validRows <1x24000 logical>
Now I need the coordinates of the elements which got a "1"
For all lines I did
X = [Polylines.X];
Y = [Polylines.Y];
How can I say "not all lines, but only the lines which have the "1" in validRows".
Do I have to transpose the validRows as it is a row vector (<1x24000 logical>) but Polylines are a column <24000x1 struct> ?
Thank you very much for your help, Claudia
Geoff
Geoff on 21 May 2012
For that you can use find:
> indices = find(validRows)
But you actually don't need to. MatLab will let you use a logical array (ie validRows) as an index, and will only return the rows where the index contains a logical '1'. That's what I meant by 'logical index'.
So this will work:
> Xfilt = X(validRows)
And so will this:
> Xfilt = X(indices)
Hello Geoff
Thank you so much!
I put my X and Y in a cell array, too, and now it works.
Quite easy if you know how it works. Now, I know that struct is a nice thing to save data but for my calculations it is not really useful.
Do you know good resources for learning things like that or is it 'just' experience how to deal with data in MATLAB?
Thank you so much for your time and help.
Claudia
Geoff
Geoff on 21 May 2012
Glad to help. As for resources... Errr... not off the top of my head. You could try www.mathworks.com/tutorials or just google various phrases related to what you want including the keyword 'tutorial' or 'example'... I kinda short-circuited the whole conceptual thing when I started using MatLab cos I'd been programming computers for over 20 years =)
You learn a lot just by asking questions here or by reading answers to other questions. I picked up quite a lot of cool MatLab stuff by answering questions here and seeing how much better other peoples' answers were! And yeah, there's a certain amount of knowledge that you only get from trying things out... So my sit-on-the-fence answer is "all of the above".
By the way, when I store data sets in MatLab I tend to use a struct of arrays instead of an array of structs. You might find that representation more useful at times... But like everything in life, each has its benefits and drawbacks.
Hi Geoff
just watched a video about struct and arrays.
I already tried to change the organization
from 'array of struct' (synonym for 'element-by-element organization', yes?)
to 'struct of array' (synonym for 'plane organization', yes?)
It did not work. I posted the question but could not get an answer. It was not me who created the file.
Do you know how I can change it afterwards?
That would be great!
I go home now and wish you a nice day or evening, wherever you are :-)
Cheers, Claudia
Hi Geoff
I woke up this morning and knew the answer :-)
Now I gave the "{ Polylines.ACCURACY} " command in a similar way to all attributes and then I created the struct again with "Polylines.ACCURACY = ACCURACY" and so on for all attributes.
Thank you for putting me in the right way!
Cheers, Claudia

Sign in to comment.

More Answers (0)

Categories

Asked:

on 21 May 2012

Community Treasure Hunt

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

Start Hunting!