how do I display all the text on one line?

Hi everyone, I have a problem: I extracted the text on matlab using str = extractFileText (filename) command, when I press on run my output in the command window gives me the text with the original formatting, while to me you need to make the text appear on a single line. How can I do? thanks in advance to those who answer me

 Accepted Answer

Adam Danz
Adam Danz on 20 Mar 2020
Edited: Adam Danz on 20 Mar 2020
Try this.
filename = 'swire.pdf';
str = extractFileText(filename);
c = regexprep(str,{char(13),char(10)}, ''); % or replace with ' '
size(c{1}) % = 1 x 204710

8 Comments

hi Adam thanks for the answer, I tried to insert the code as you wrote, but I need to insert all the text of the pdf file in only one line (to display in command window) and with this command it doesn't work for this. could you still help me? :/ thanks
Adam Danz
Adam Danz on 20 Mar 2020
Edited: Adam Danz on 20 Mar 2020
Have you tried this? I downloaded the pdf you shared and it works.
As you can see below, it's one line.
What's this??
c = regexprep(str,{char(13),char(10)}, 'swire'); % or replace with ' '
% ^^^^^^
Do you realize what you're doing with that line?
You're replacing the characters that are causing additional lines with the word "swire". I doubt that's what you want to do.
What's wrong with that result? It's one line of text which is what you described.
thanks too much Adam
Here's how to break it up into 10 lines.
% Get index of space
spaceIdx = regexp(c,' +'); % index of spaces
nLines = 10; % number of lines
nChars = strlength(c); % number of characters; alternative: numel(c{1})
nCharPerLine = round(nChars / nLines); % approximate number of chars per line
breakPoints = linspace(nCharPerLine, nChars, 10); % approximate break indices
[~, minIdx] = min(abs(spaceIdx(:) - breakPoints(1:end-1)));
c{1}(spaceIdx(minIdx)) = newline
Thanks with the heart, Adam I solved all the problems. You have been precious to me!
Glad I could help.

Sign in to comment.

More Answers (1)

Did you try removing the newlines (or other formatting characters)?

5 Comments

hi monika thanks for the answer, but I need to see all that is written in the pdf in the command window in one line, can you still help me? thank you
What is the problem you are seeing? This solution in the above link works for me. In the image below I have a multi-line string str that is dispalyed at a single line of text.
oh monika always thanks for the answer, but being the text too long it truncates, so how can I make more lines (about 10lines) ? thanks a lot
I tried printing text that is 100 lines and I did not experience a problem. Please provide the file that you are using.
ok monika the file is swire and it is about 100 pages ...thanks always

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!