strfind returns empty cell despite that the strings contain same characters

3 次查看(过去 30 天)
I have big data file which I mapped using memmapfile and then change it to char. then I randomly generate a number and check whether the data contain this number using strfind. Although the generated number is within the data, command returns empty array. datamap=memmapfile('file.txt'); datachar=transpose(char(datamap.Data)); r = (r2-r1).*rand(1,1) + r1; %random number in range r1 r2 strfind(num2str(r),datachar)
I also tried sprintf to make sure both inputs are strings. when I inserted sprintf(datachar) I got warning: Control Character '\' is not valid. See 'doc sprintf' for control characters valid in the format string. ... not sure if this can cause the problem. the data set contains numbers, strings, \t for tabulator, \n for end of the row and \N which should be handled as string but might cause a problem...? Thanks for any suggestions.

回答(1 个)

Ronit
Ronit 2024-6-21
Hi,
It looks like the issue you're encountering is due to the parameters being passed to the ‘strfind function in the wrong order. The strfind function expects the first parameter to be the string in which to search and the second parameter to be the substring to search for. Here's how you can correct your code:
% Correct the order of parameters in strfind
index = strfind(num2str(r), datachar);
Here is the documentation link of strfind function for reference - https://www.mathworks.com/help/matlab/ref/strfind.html
Additionally, if you encounter any control characters or special characters that need to be handled, you may need to preprocess datachar to clean or escape these characters appropriately.
Hope it helps!
  2 个评论
DGM
DGM 2024-6-21
The example you gave is still wrong. It's identical to the code as given.
The less obvious problem that will almost certainly guarantee that this won't work is the blind usage of num2str(). Unless the text in the file is formatted exactly as num2str() formats it, there's no reason to assume that a random float numeric value will match some text in a file with unknown number formatting.
num2str(100.0001)
ans = '100.0001'
num2str(100.00001)
ans = '100'
num2str(0.00001)
ans = '1e-05'
Of course, we don't know what r1 and r2 are, and we don't know what's in the file either.
Stephen23
Stephen23 2024-6-21
In addition to DGM‘s comment: rather than inefficient type conversion followed by fragile text comparison, why not simply and efficiently perform the comparison of numeric data in the numeric domain ?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by