strfind returns empty cell despite that the strings contain same characters
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (1)
Ronit
el 21 de Jun. de 2024
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 comentarios
DGM
el 21 de Jun. de 2024
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)
num2str(100.00001)
num2str(0.00001)
Of course, we don't know what r1 and r2 are, and we don't know what's in the file either.
Stephen23
el 21 de Jun. de 2024
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 ?
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!