Help Needed: Fixing Indexing Error in MATLAB Random Name Generator Code
2 ビュー (過去 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 日
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 日
編集済み: Les Beckham
2024 年 8 月 13 日
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 日
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 日
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!