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

Geoff Hayes
Geoff Hayes 2014년 11월 26일
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
Bharat 2014년 11월 27일
Sorry Geoff, I am new to matlab and this site. I apologize that I didn't post the query properly.
Image Analyst
Image Analyst 2014년 11월 27일
편집: Image Analyst 2014년 11월 27일
Well you didn't have a closing apostrophe on the "if" line. But why not just use my answer?

댓글을 달려면 로그인하십시오.

 채택된 답변

per isakson
per isakson 2014년 11월 27일
편집: per isakson 2014년 11월 27일

1 개 추천

if file(i,1)=='GDD51' &nbsp will not work for two reasons
Firstly, the comparison is made position by position. The result is a vector of logical. To assert identical strings all must be true.
>> 'ABC'=='AAA'
ans =
1 0 0
>> all( 'ABC'=='AAA' )
ans =
0
However,&nbsp if&nbsp of recent Matlab versions applies an implicit&nbsp all. &nbsp That was not always the case(?). See if, elseif, else
[...] An expression is true when its result is nonempty and contains
only nonzero elements (logical or real numeric).
Secondly, the strings must have the same length else an error is thrown
>> 'ABC'=='AABCA'
Error using ==
Matrix dimensions must agree.
I think, the best code is (not tested)
>> newfile = file( strcmp( 'GDD51', file(:,1) ), 2 );

댓글 수: 3

Bharat
Bharat 2014년 11월 27일
thanks per isakson! I actually still used the for loop but with strcmp!
Image Analyst
Image Analyst 2014년 11월 27일
Any reason why you chose not to use ismember(), which is how I think most MATLABers would do it?
per isakson
per isakson 2014년 11월 27일
편집: per isakson 2014년 11월 27일
According to my old rule of thumb
ism = strcmp( str, cell_array_of_strings );
is significantly faster than
ism = ismember( str, cell_array_of_strings );
(This is a special case to ismember)
However, that might have changed.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 11월 26일

1 개 추천

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

Image Analyst
Image Analyst 2014년 11월 27일
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);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2014년 11월 26일

편집:

2014년 11월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by