match a word/string
Show older comments
Hi all. I have a file, in which the first column which contains 27k rows of words/strings. (eg:GDD51). I need to search a particular string and create another file with all values corresponding to the second column in that file. The problem is, i can't search for a string. It says error! Can someone help me. this is the code i used:
x=0;
for i=1:27099
if file(i,1)=='GDD51
x=x+1;
newfile(x,1)=file(i,2);
end
end
And i get this error: Undefined function 'eq' for input arguments of type 'table'. Please help,Thanks
3 Comments
Geoff Hayes
on 26 Nov 2014
Bharat - please post the code that you are using to read the data from file, and search for the string that you are interested in. As well, since there is an error, please post the full error message (all text in red). Just update/edit your above question with thus additional information.
Bharat
on 27 Nov 2014
Image Analyst
on 27 Nov 2014
Edited: Image Analyst
on 27 Nov 2014
Well you didn't have a closing apostrophe on the "if" line. But why not just use my answer?
Accepted Answer
More Answers (1)
Image Analyst
on 26 Nov 2014
Try this:
% Create sample data
cellArrayOfStrings = {'No Match this index', 'GDD51', 'no match here either'};
% Find index where cell contents = 'GDD51'
index = ismember(cellArrayOfStrings, 'GDD51')
1 Comment
Image Analyst
on 27 Nov 2014
Regarding your edit for making a file of the second column where it's true
fid = fopen(filename, 'wt');
for k = 1 : length(index)
if index(k)
fprintf(fid, '%s\n', cellArrayOfStrings{k});
end
end
fclose(fid);
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!