Print OCR results to text file
14 views (last 30 days)
Show older comments
Hello,
I have read the other posts and examples for printing your OCR results into a text file but am running into an error.
Error using fprintf
Function is not defined for 'cell' inputs.
Error in REMOVEDallEXTRAbits (line 142)
fprintf(fileID, '%s\r\n', text);
ocrtxt = ocr(BW2, textBBoxes, 'Language', 'C:\Users\e362609\Documents\MATLAB\Examples\vision\TextDetectionExample\try50\tessdata\try50.traineddata');
% [ocrtxt.Text];
fileID = fopen('trytext.txt');
fprintf(fileID, '%s\r\n', text);
fclose(fileID);
Any help is much apreciated!
2 Comments
Star Strider
on 7 Jul 2019
I don’t have the Computer Vision Toolbox, so I have no personal experience with it.
However (according to the online documentation on ocr), it appears that the information you want is either in the structure fields:
ocrtext.Text
that appears to return a character array, or:
ocrtext.Words
that returns a cell array.
Consider that:
ocrtxt.Words = {'This','is','a','test'}; % Create Something Similar To What I Believe ‘ocr’ Returns
fprintf(f,'%s\n',ocrtxt.Words{:})
produces:
This
is
a
test
and no loop is necessary.
See if that works with your data.
Accepted Answer
More Answers (2)
dpb
on 6 Jul 2019
To write cell output with fprintf, you must dereference the cell content "use the curlies, Luke!".
fprintf(fileID, '%s\r\n', text{:});
or, use the new writecell if you have R2019a.
6 Comments
dpb
on 6 Jul 2019
Edited: dpb
on 7 Jul 2019
AHA! The one about "Expected one output from a curly brace or dot indexing expression, but there were 544 results." is helpful! ocrtxt must be a struct and the Text field a cellstr array.
When we dereferenced with {:}, that gets the whole array as a comma-separated list--there apparently are actually 544 lines in the text you got back.
You need to output each cellstr...
for i=numel(ocr.Text)
fprintf(fid,'%s\n',ocr.Text{i})
end
should provide a pathway to nirvana...presuming, of course, you've opened fid for write operations first...
ADDENDUM
The whos line showed an array of the object and so it isn't a single object with array of strings as the above presumes but an array with one string per element. So iterate over the array of objects instead of a (presumed) cellstr array..
for i=numel(ocrtxt)
fprintf(fid,'%s\n',ocrtxt(i).Text)
end
If the doc is correct, this should NOT cause any issues about cell element in fprintf
9 Comments
See Also
Categories
Find more on String Parsing 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!