Printing sentences using cell arrays

Given: 3 names, 3 verbs, 3 nouns; how to initalize a cell array.
Find: I need to print random elements from each of the above terms in a cell array to make a sentence.
Issue: I can initialize the cell arrays, (I think), but I am struggling to print the sentences in the command window...
My Solution: I initialize a cell array of 1 row and 3 columns, but I'm struggling to figure out how to randomize and print them.
C=cell(1,3);
Names={'Harry','Xavier','Sue'};
Verbs={'loves','eats','throws'};
Nouns={'baseballs','rocks','pizza'};
% I tried creating a variable rand, and stating rand=rand(C(Names, Verbs,
% Nouns)) such that it filled the empty cell array with random elements
% from the names, verbs, and nouns and got the error !!!Unable to use a value of type cell as an index.
% I'd then I think use the fprintf function to display the value of C?

 Accepted Answer

% Define cell arrays
Names = {'Harry', 'Xavier', 'Sue'};
Verbs = {'loves', 'eats', 'throws'};
Nouns = {'baseballs', 'rocks', 'pizza'};
% Generate and print a random sentence
fprintf('%s %s %s.\n', Names{randi(numel(Names))}, Verbs{randi(numel(Verbs))}, Nouns{randi(numel(Nouns))});
Harry loves pizza.
@Kyle Weaver By accessing the cell array elements directly in the fprintf statement, you can reduce the overhead of temporary variables.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

9 Comments

% Define cell arrays
Names = {'Harry', 'Xavier', 'Sue'};
Verbs = {'loves', 'eats', 'throws'};
Nouns = {'baseballs', 'rocks', 'pizza'};
% Creating a sentence using the selected random elements
sentence = sprintf('%s %s %s.\n', Names{randi(numel(Names))}, ...
Verbs{randi(numel(Verbs))}, ...
Nouns{randi(numel(Nouns))});
% Printing the sentence
fprintf('%s\n', sentence);
Xavier loves rocks.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
This might be a daft question... But what does the "..." after Names{randi(numel(Names))}, ... & Verbs{randi(numel(Verbs))}, ... mean?
I see. If this just for appearance sake? As opposed to just writing as : Names{randi(numel(Names))}, Verbs{randi(numel(Verbs))}, Nouns{randi(numel(Nouns))});
Stephen23
Stephen23 on 15 Apr 2024
Edited: Stephen23 on 15 Apr 2024
"If this just for appearance sake?"
There is nothing stopping you from writing an entire function all on one line, it really makes no difference to your computer. However keeping lines around 80 characters** long seems to be about right length for us humans to be able to scan and comprehend. Thus being able to continue code on the next line is sometimes useful.
** give or take a bit, some people have strong opinions on what is the "correct" length.
In the case I use ellipses, is there a way to properly indent it so it looks as Mr. Hassaan has it? I suppose smart indent doesn't work for that purpose, like I thought it would.
"is there a way to properly indent it so it looks as Mr. Hassaan has it?"
Any definition of "properly" is subjective.
When you first add ellipsis then the next line will use your default indent (usually four spaces). Simply manually change the indent by adding spaces and then any further ellipses will use your manual indent depth:
A = [1,2,3,...
4,5,6,... default indent
7,8,9,... manually modified indent to 9 spaces
10,11,... automatically indented to 9 spaces
12,13,... automatically indented to 9 spaces
14,15]; % automatically indented to 9 spaces
You can also select multiple lines and press the TAB button. This is all explained in the documentation:
You can also use ctrl+] or ctrl+[ to increase or decrease, respectively, the tab level of one or more lines of code.
Thank you. I learn so many new things every day on MATLAB! :)

Sign in to comment.

More Answers (1)

Voss
Voss on 14 Apr 2024
Edited: Voss on 14 Apr 2024
Here's a way to generate multiple random sentences at once:
Names={'Harry','Xavier','Sue','Howard','Fred'}; % adding two names and one noun
Verbs={'loves','eats','throws'}; % to show that these don't all
Nouns={'baseballs','rocks','pizza','iPads'}; % have to be the same length
% number of sentences to print:
N = 12;
C = [ ...
Names(randi(end,[1 N])); ...
Verbs(randi(end,[1 N])); ...
Nouns(randi(end,[1 N])); ...
]
C = 3x12 cell array
{'Howard'} {'Fred' } {'Fred' } {'Sue' } {'Harry' } {'Howard'} {'Xavier' } {'Sue' } {'Sue' } {'Fred' } {'Xavier'} {'Xavier'} {'throws'} {'throws'} {'eats' } {'eats' } {'throws' } {'loves' } {'loves' } {'throws' } {'loves'} {'throws'} {'eats' } {'throws'} {'iPads' } {'rocks' } {'pizza'} {'rocks'} {'baseballs'} {'pizza' } {'baseballs'} {'baseballs'} {'rocks'} {'pizza' } {'iPads' } {'iPads' }
fprintf('%s %s %s.\n',C{:})
Howard throws iPads. Fred throws rocks. Fred eats pizza. Sue eats rocks. Harry throws baseballs. Howard loves pizza. Xavier loves baseballs. Sue throws baseballs. Sue loves rocks. Fred throws pizza. Xavier eats iPads. Xavier throws iPads.
And here's one way to generate all possible sentences of the specified form:
T = combinations(Names,Verbs,Nouns)
T = 60x3 table
Names Verbs Nouns __________ __________ _____________ {'Harry' } {'loves' } {'baseballs'} {'Harry' } {'loves' } {'rocks' } {'Harry' } {'loves' } {'pizza' } {'Harry' } {'loves' } {'iPads' } {'Harry' } {'eats' } {'baseballs'} {'Harry' } {'eats' } {'rocks' } {'Harry' } {'eats' } {'pizza' } {'Harry' } {'eats' } {'iPads' } {'Harry' } {'throws'} {'baseballs'} {'Harry' } {'throws'} {'rocks' } {'Harry' } {'throws'} {'pizza' } {'Harry' } {'throws'} {'iPads' } {'Xavier'} {'loves' } {'baseballs'} {'Xavier'} {'loves' } {'rocks' } {'Xavier'} {'loves' } {'pizza' } {'Xavier'} {'loves' } {'iPads' }
C = table2array(T).';
fprintf('%s %s %s.\n',C{:})
Harry loves baseballs. Harry loves rocks. Harry loves pizza. Harry loves iPads. Harry eats baseballs. Harry eats rocks. Harry eats pizza. Harry eats iPads. Harry throws baseballs. Harry throws rocks. Harry throws pizza. Harry throws iPads. Xavier loves baseballs. Xavier loves rocks. Xavier loves pizza. Xavier loves iPads. Xavier eats baseballs. Xavier eats rocks. Xavier eats pizza. Xavier eats iPads. Xavier throws baseballs. Xavier throws rocks. Xavier throws pizza. Xavier throws iPads. Sue loves baseballs. Sue loves rocks. Sue loves pizza. Sue loves iPads. Sue eats baseballs. Sue eats rocks. Sue eats pizza. Sue eats iPads. Sue throws baseballs. Sue throws rocks. Sue throws pizza. Sue throws iPads. Howard loves baseballs. Howard loves rocks. Howard loves pizza. Howard loves iPads. Howard eats baseballs. Howard eats rocks. Howard eats pizza. Howard eats iPads. Howard throws baseballs. Howard throws rocks. Howard throws pizza. Howard throws iPads. Fred loves baseballs. Fred loves rocks. Fred loves pizza. Fred loves iPads. Fred eats baseballs. Fred eats rocks. Fred eats pizza. Fred eats iPads. Fred throws baseballs. Fred throws rocks. Fred throws pizza. Fred throws iPads.

7 Comments

Eureka! These are all brilliant ways to generate multiple sentences at once... If I just wanted to generated one sentence at a time could I change N in your first example to 1?
"could I change N in your first example to 1?"
I think so. Why don't you try it and see what happens?
It works! :) Genius!
Thank you very much!
You're welcome!

Sign in to comment.

Categories

Products

Release

R2024a

Asked:

on 14 Apr 2024

Commented:

on 24 Apr 2024

Community Treasure Hunt

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

Start Hunting!