Help Needed: Fixing Indexing Error in MATLAB Random Name Generator Code
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (4)
Voss
el 13 de Ag. de 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 comentarios
Les Beckham
el 13 de Ag. de 2024
Editada: Les Beckham
el 13 de Ag. de 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 comentarios
Steven Lord
el 13 de Ag. de 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 comentarios
Image Analyst
el 13 de Ag. de 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 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!