Help Needed: Fixing Indexing Error in MATLAB Random Name Generator Code

31 次查看(过去 30 天)
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!

回答(4 个)

Voss
Voss 2024-8-13,16:03
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]);
Generated Name: Riley Jones

Les Beckham
Les Beckham 2024-8-13,16:06
编辑:Les Beckham 2024-8-13,16:12
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]);
{'Generated Name: '} {'Jordan'} {' '} {'Smith'}
nameFixed = generateRandomNameFixed();
disp(['Generated Name: ' nameFixed])
Generated Name: Morgan Williams
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

Steven Lord
Steven Lord 2024-8-13,17:29
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
Now serving customer: Riley Williams Now serving customer: Alex Jones Now serving customer: Jordan Johnson Now serving customer: Jordan Jones Now serving customer: Riley Johnson Now serving customer: Riley Jones Now serving customer: Alex Jones Now serving customer: Morgan Brown Now serving customer: Jordan Jones Now serving customer: Taylor Smith
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

Image Analyst
Image Analyst 2024-8-13,20:34
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 ( ).

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by