How can I print large paragraphs of text?

28 views (last 30 days)
%Ive attached the very first part of my code, but the rest of the code is the same way.
%I want to stop using disp to format my paragraphs. should I just use fprintf? if so how
%can I include multiple lines in one statement? Sorry for newbie question.
name=input('Enter your characters name: ','s');
fprintf('Welcome %s, theres a long journey ahead of you.\n',name)
disp('You will need to choose a class first')
class=menu('CLASS', 'Soldier', 'Mage', 'Drunk', 'Scoundrel');
if class==1
disp('You have chosen a soilder.')
disp('You display a fierce heart of courage yet are somewhat dimwitted.')
disp('Your armor is freshly polished, your sword sharpened to a razor.')
disp('In your pocket you find a small stone totem. When it touches your')
disp('skin you feel a cool electricity beneath it.')
elseif class==2
disp('You have chosen a mage.')
disp('Your knowledge of alchemy has put you above your predecessor,')
disp('but you have become arrogant. You are cunning and often disregard')
disp('morals when conducting experiments. In your pocket you find a vile')
disp('and a poorly sharpened dagger with a leather bound handle.')
elseif class==3
disp('You have chosen drunk.')
disp('Your skin is tough and wrinkled from dock work in the sun,')
disp('Hair asunder and roughly parted, you find yourself quite drunk.')
disp('You reach into your pocket and find a small bottle of rum given ')
disp('to you by a local group of pirates.')
elseif class==4
disp('You have chosen Scoundrel.')
disp('Your eyes are filled with greed and your motives are unknown.')
disp('You lust knows no bounds, and your morals can be shown by your ')
disp('distraught appearance. You reach into your pocket and find a ')
disp('piece of torn cloth and a welll sharpened stolen dagger.')

Accepted Answer

Jan
Jan on 9 Mar 2021
Edited: Jan on 9 May 2022
Do not store large portions of text inside the M-file. Use a text file or a set of text files and import them dynamically. Separating code, data and GUI is essential for improving the quality of the program.
In your case one text file might be sufficient with e.g. an empty line as separator or a specific character like "§". Then import the file by fileread, join the lines smartly by inserting spaces on demand, and split the text at the special character:
function C = ImportMyText(FileName)
S = fileread(FileName);
S(S == char(13)) = []; % DOS linebreaks to unix linebreaks
S(S == char(10)) = ' '; % Replace linebreaks by spaces
S = strrep(S, ' ', ' '); % Replace double spaces by single space
C = strsplit(S, '§'); % [EDITED] Typo fixed: C -> S (Thanks Sean)
end
Now instead of the huge list of text in the source code, you code will look like:
class = menu('CLASS', 'Soldier', 'Mage', 'Drunk', 'Scoundrel');
C = ImportMyText('You\File\name.txt');
disp(C{class});
Of course you need some fine tuning, e.g. that the first sentence is separated. Limiting the text width to a wanted number of characters needs to be solved also. In a GUI this is done by textwrap.
  2 Comments
taryn dewey
taryn dewey on 9 Mar 2021
Thanks for taking the time to answer my question. This makes so much more sense. My coding experience is limited to a couple electives I took as an undergrad. I appreciate it, and will stop spamming my source code with massive blocks of text.
Sean Brennan
Sean Brennan on 20 Jun 2021
Edited: Sean Brennan on 20 Jun 2021
There's a minor typo in the answer given above. The line:
C = strsplit(C, '§');
Should be:
C = strsplit(S, '§');
Otherwise, the example is a nice one, particularly in showing a quick way to clean out the Windows/Unix line break headaches.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!