Help Needed: Fixing Indexing Error in MATLAB Random Name Generator Code
2 views (last 30 days)
Show older comments
I am working on developing a name generator tool in MATLAB to produce random names for individuals or animals. I am inspired by the functionality of the website nameswhisperer.com and aim to create a similar tool.
I have written the following MATLAB code to generate random names:
function randomName = generateRandomName()
% Define lists of name components
firstNames = {'Alex', 'Jordan', 'Taylor', 'Riley', 'Morgan'};
lastNames = {'Smith', 'Johnson', 'Williams', 'Brown', 'Jones'};
% Generate random indices
firstNameIndex = randi(length(firstNames));
lastNameIndex = randi(length(lastNames));
% Construct random name
randomName = [firstNames(firstNameIndex) ' ' lastNames(lastNameIndex)];
end
% Example usage
name = generateRandomName();
disp(['Generated Name: ' name]);
However, I am encountering an issue with the code. Specifically, when I run the script, I receive an error related to the way names are indexed and concatenated.
Could you help identify and correct the mistake in the code?
Thank you for your assistance!
0 Comments
Answers (4)
Voss
on 13 Aug 2024
I'm not sure what the error is, but you should use curly braces {} to get the contents of a cell in a cell array.
function randomName = generateRandomName()
% Define lists of name components
firstNames = {'Alex', 'Jordan', 'Taylor', 'Riley', 'Morgan'};
lastNames = {'Smith', 'Johnson', 'Williams', 'Brown', 'Jones'};
% Generate random indices
firstNameIndex = randi(length(firstNames));
lastNameIndex = randi(length(lastNames));
% Construct random name
randomName = [firstNames{firstNameIndex} ' ' lastNames{lastNameIndex}];
% ^ ^ ^ ^ curly braces
end
% Example usage
name = generateRandomName();
disp(['Generated Name: ' name]);
0 Comments
Les Beckham
on 13 Aug 2024
Edited: Les Beckham
on 13 Aug 2024
You need to use curly braces instead of parentheses to extract the contents of the cell array of char vectors for the names.
% Example usage
name = generateRandomName();
disp(['Generated Name: ' name]);
nameFixed = generateRandomNameFixed();
disp(['Generated Name: ' nameFixed])
function randomName = generateRandomName()
% Define lists of name components
firstNames = {'Alex', 'Jordan', 'Taylor', 'Riley', 'Morgan'};
lastNames = {'Smith', 'Johnson', 'Williams', 'Brown', 'Jones'};
% Generate random indices
firstNameIndex = randi(length(firstNames));
lastNameIndex = randi(length(lastNames));
% Construct random name
randomName = [firstNames(firstNameIndex) ' ' lastNames(lastNameIndex)];
end
function randomName = generateRandomNameFixed()
% Define lists of name components
firstNames = {'Alex', 'Jordan', 'Taylor', 'Riley', 'Morgan'};
lastNames = {'Smith', 'Johnson', 'Williams', 'Brown', 'Jones'};
% Generate random indices
firstNameIndex = randi(length(firstNames));
lastNameIndex = randi(length(lastNames));
% Construct random name
randomName = [firstNames{firstNameIndex} ' ' lastNames{lastNameIndex}]; % <<< use curly braces
end
0 Comments
Steven Lord
on 13 Aug 2024
Others have suggested using curly braces to extract the contents of a cell from the cell arrays firstNames and lastNames. Another option would be to make those two variables string arrays instead of cell arrays. If you did that, assembling the name from the two pieces could use string appending with the + operator. This would also require a slight change to how you use the generated name, also using + for appending.
% Example usage
for whichname = 1:10 % line added
name = generateRandomName();
disp('Now serving customer: ' + name); % line changed
end % line added
function randomName = generateRandomName()
% Define lists of name components
firstNames = ["Alex", "Jordan", "Taylor", "Riley", "Morgan"]; % line changed
lastNames = ["Smith", "Johnson", "Williams", "Brown", "Jones"]; % line changed
% Generate random indices
firstNameIndex = randi(length(firstNames));
lastNameIndex = randi(length(lastNames));
% Construct random name
randomName = firstNames(firstNameIndex) + ' ' + lastNames(lastNameIndex); % line changed
end
0 Comments
Image Analyst
on 13 Aug 2024
See the FAQ:
It will give you a good intuitive feel for when to use braces { }, when to use brackets [ ], and when to use parentheses ( ).
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!