Compare a string to all values in column and extract rows where such Strings are found

3 Ansichten (letzte 30 Tage)
There all I need help. I have a string that am comparing to all other strings in a column of an n-by-5 matrix. I thought its that simple but the extraction has empty matrix. I am at a fix here I need assistance. I loaded a .'mat' file containing the table and tried to extract according to the code below:
load('ds_nyc.mat');
NYCdata_extract=ds_nyc(:,[1 4:6 8]);
n=size(NYCdata_extract,1);
getBank=[];
tf=[];
for x=1:n
tf=strcmp('NYCdata_extract(x,2)','Bank');
if (tf==1)
getBank(x,:)=NYCdata_extract(x,:);
end
end
Any assistance will be deeply appreciated. Please see the attached file for clarity of the what I wanted to do.

Antworten (2)

Azzi Abdelmalek
Azzi Abdelmalek am 23 Mai 2016
idx=ismember(NYCdata_extract(x,2),'Bank')
out=NYCdata_extract(idx,:)
  1 Kommentar
VicPeters
VicPeters am 23 Mai 2016
Thanks Azzi for the speedy reply. But it extracted just about 20 zeros or ones. The ones represents that 'Bank' was found at those location. However, I checked the table, entry for 'Bar' for instance, it was 15000+ but only same 20 results were produced. The ones are at the same location as in 'Bank' output.
Moreover, I will like to extract the entire rows where strings are found. But currently the array was shown as empty array. Is there any way to modify the code to these things?

Melden Sie sich an, um zu kommentieren.


Guillaume
Guillaume am 23 Mai 2016
tf=strcmp('NYCdata_extract(x,2)','Bank');
compares the string 'NYCdata_extract(x,2)' to the string 'Bank', not the content of NYCdata_extract(x,2). Since the two strings are not the same, tf is always going to be false. The correct instruction should have been
tf = strcmp(NYCdata_extract(x,2),'Bank'); %no quote around the variable name.
Of course, since strcmp can compare a whole column of strings with a single string, the loop is completely unnecessary. All that is needed is:
load('ds_nyc.mat');
NYCdata_extract = ds_nyc(:,[1 4:6 8]);
getBank = NYCdata_extract(strcmp(NYCdata_extract(:, 2),'Bank'), :)
  2 Kommentare
VicPeters
VicPeters am 23 Mai 2016
Return empty matrix. But I've tried to move around it by extracting outside matlab and importing as csv file. Thanks
Guillaume
Guillaume am 23 Mai 2016
I missed that you were using a dataset. As far as I know, the only way to extract the column of a dataset as a cell array is to use the column name:
load('ds_nyc.mat');
NYCdata_extract = ds_nyc(:,[1 4:6 8]);
getBank = NYCdata_extract(strcmp(NYCdata_extract.category_name,'Bank'), :)
Note that dataset is deprecated and you should be using tables instead.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by