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!
0 个评论
回答(4 个)
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]);
0 个评论
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]);
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 个评论
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
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 个评论
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 ( ).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!