Print OCR results to text file
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Laura Elder
am 5 Jul. 2019
Beantwortet: Laura Elder
am 7 Jul. 2019
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 Kommentare
Star Strider
am 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.
Akzeptierte Antwort
Weitere Antworten (2)
dpb
am 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 Kommentare
dpb
am 6 Jul. 2019
Bearbeitet: dpb
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu String Parsing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!