How do I make a loop that will allow to add a song again and saving the previos one

1 vue (au cours des 30 derniers jours)
fprintf(' Let''s get started! \n');
% Define variables.
RepreProcessA = "No";
RepreProcessD = "No";
RepreProcessE = "No";
SongFound = 0;
RepreProcessAll = "Y";
EditAgain = "Yes";
ModSelection = input(' Will you like to sign in as a User or Administrator? ','s');
while strcmpi(ModSelection ,'User') ~=1 && strcmpi(ModSelection ,'Administrator') ~=1
fprintf(' You have selected a not existing option. \n');
ModSelection = input(' Are you a User or an Administrator ','s');
end
if strcmpi(ModSelection ,'Administrator')
fprintf(' Welcome to the MusicLive hardcore. \n');
% admname = input(' What is your Username? ','s');
% while admname ~= "AdmMusicLive"
% fprintf(' Incorrect username! \n');
% admname = input(' What is your username, NERD!!! ','s');
% end
% fprintf(' Username correct \n');
% admpassword = input(' What is your password? ','s');
% while admpassword ~= "AdmPassword"
% fprintf(' Incorrect password! \n');
% admpassword = input(' What is the correct password, NERD!!! ','s');
% end
% fprintf(' Password correct \n');
% fprintf(' %s, you have successfully entered into hardcode of the software. \n', User_Name);
% fprintf(' You will have access to Add, Delete and Edit the software database. \n');
OptSelection = input(' Will you like to Add, Delete, or Edit a song? ', 's');
while strcmpi(OptSelection,'Add') ~=1 && strcmpi(OptSelection,'Delete') ~=1 ...
&& strcmpi(OptSelection,'Edit') ~=1
fprintf(' You have selected a not existing option \n');
OptSelection = input(' Selected a valid option ','s');
end
%--------------------------------------------------------------------------------
if strcmpi(OptSelection,"Add")
while strcmpi(RepreProcessAll,"Y")
while strcmpi(RepreProcessA,"No")
SongName = input(" What is the song's name? ",'s');
while isempty(SongName)
fprintf(' Invalid name, enter a name with only alphabetical characters \n');
SongName = input(" What is the song's name? ",'s');
end
fprintf(" The name of the new songs was entered correctly \n");
fprintf(" \n");
SongSinger = input(' How is the song''s singer ','s');
while isempty(SongSinger)
fprintf(' Invalid name, enter a name with only alphabetical characters \n');
SongSinger= input(" How is the song''s singer ",'s');
end
fprintf(" The singer of the new songs was entered correctly \n");
fprintf(" \n");
SongTime = input(" What is the song's length in minutes (round to the nearest whole number)? ");
while isempty(SongTime) || SongTime < 0
fprintf(' Error, the value entered is incorrect \n');
SongTime = input(' Please, enter a whole number ');
end
if SongTime <= 2
SongTime = "ShortOpt";
elseif SongTime >= 3
SongTime = "IntermidianOpt";
elseif SongTime >= 4
SongTime = "LongOpt";
end
fprintf(' The new song time was entered successfully. \n');
fprintf(" \n");
fprintf(" Enter the song genre. \n");
fprintf(" The three options for the song's genre are Rock, Pop, and Country. \n");
SongGenre = input(" What is the song's genre? ",'s');
while strcmpi(SongGenre,'Rock') ~=1 && strcmpi(SongGenre,'Pop') ~=1 ...
&& strcmpi(SongGenre,'Country') ~=1
fprintf(' You have selected a not existing option \n');
fprintf(' Selected Rock, Pop, or Country. \n');
SongGenre = input(' Selected the correct option ','s');
fprintf(' The new song genre was entered successfully. \n');
end
fprintf(" The genre of the new songs was entered correctly. \n");
fprintf(" \n");
fprintf(" The three options for the song's language are English, Italian, and Romanian. \n");
SongLanguage = input(" What is the language of the song? ", 's');
while strcmpi(SongLanguage,'English') ~=1 && strcmpi(SongLanguage,'Italian') ~=1 ...
&& strcmpi(SongLanguage,'Romanian') ~=1
fprintf(' You have selected a not existing option NERD!!! \n');
fprintf(' Selected English, Italian, or Romanian. \n');
SongLanguage = input(' Selected the correct option ','s');
end
fprintf(' The new song language was entered successfully. \n');
fprintf(" \n");
fprintf(' The song name is %s \n', SongName);
fprintf(' The song singer is %s \n', SongSinger);
fprintf(' The song time is %s \n', SongTime);
fprintf(' The song genre is %s \n', SongGenre);
fprintf(' The song language is %s \n', SongLanguage);
fprintf(' Selected Yes if desired to save the changes; if not selected No \n');
RepreProcessA = input(' Yes or No ','s' );
while strcmpi(RepreProcessA, 'No') ~= 1 && strcmpi(RepreProcessA, 'Yes') ~= 1
fprintf(' Selected Yes or No NERD!!! \n');
RepreProcessA = input(' Selected Yes if desired to save the changes; if not selected No ','s');
end
NewData = [MusicDatabase; {SongName}, {SongSinger} {SongTime}, {SongGenre}, {SongLanguage}];
writecell(NewData,'Music_Database.xlsx')
end
RepreProcessAll = input(' Selected Y if desired to save the changes; if not selected N ','s');
while strcmpi(RepreProcessAll, 'N') ~= 1 && strcmpi(RepreProcessAll, 'Y') ~= 1
fprintf(' Selected Y or N NERD!!! \n');
RepreProcessAll = input(' Y if desired to save the changes; if not selected N ','s');
% This is not working, I need a obtion that will allow me more songs amd saving the ones that already add.
end
end

Réponse acceptée

Adam Drake
Adam Drake le 13 Mar 2023
Modifié(e) : Adam Drake le 14 Mar 2023
Fixed your reselection problem and simplified saving to excel file. Tried to use your coding style. See comments for clarification. Note: If this answers your question please choose to Accept the Answer.
fprintf(' Let''s get started! \n');
% Define variables.
SongFound = 0;
exitflag = false;
songcounter = 0;
ModSelection = input(' Would you like to sign in as a User or Administrator? ','s');
while strcmpi(ModSelection ,'User') ~=1 && strcmpi(ModSelection ,'Administrator') ~=1
fprintf(' You have selected a not existing option. \n');
ModSelection = input(' Are you a User or an Administrator ','s');
end
if strcmpi(ModSelection ,'Administrator')
fprintf(' Welcome to the MusicLive hardcore. \n');
% admname = input(' What is your Username? ','s');
% while admname ~= "AdmMusicLive"
% fprintf(' Incorrect username! \n');
% admname = input(' What is your username, NERD!!! ','s');
% end
% fprintf(' Username correct \n');
% admpassword = input(' What is your password? ','s');
% while admpassword ~= "AdmPassword"
% fprintf(' Incorrect password! \n');
% admpassword = input(' What is the correct password, NERD!!! ','s');
% end
% fprintf(' Password correct \n');
% fprintf(' %s, you have successfully entered into hardcode of the software. \n', User_Name);
% fprintf(' You will have access to Add, Delete and Edit the software database. \n');
% Eliminated extraneous while loops. Have user select from menu each
% time.
while ~exitflag
OptSelection = input(' Wound you like to Add, Delete, or Edit a song? ', 's');
while strcmpi(OptSelection,'Add') ~=1 && strcmpi(OptSelection,'Delete') ~=1 ...
&& strcmpi(OptSelection,'Edit') ~=1
fprintf(' You have selected a not existing option \n');
OptSelection = input(' Selected a valid option ','s');
end
%--------------------------------------------------------------------------------
if strcmpi(OptSelection,"Add")
SongName = input(" What is the song's name? ",'s');
while isempty(SongName)
fprintf(' Invalid name, enter a name with only alphabetical characters \n');
SongName = input(" What is the song's name? ",'s');
end
fprintf(" The name of the new songs was entered correctly \n");
fprintf(" \n");
SongSinger = input(' Who is the song''s singer ','s');
while isempty(SongSinger)
fprintf(' Invalid name, enter a name with only alphabetical characters \n');
SongSinger= input(" How is the song''s singer ",'s');
end
fprintf(" The singer of the new songs was entered correctly \n");
fprintf(" \n");
SongTime = input(" What is the song's length in minutes (round to the nearest whole number)? ");
while isempty(SongTime) || SongTime < 0
fprintf(' Error, the value entered is incorrect \n');
SongTime = input(' Please, enter a whole number ');
end
if SongTime <= 2
SongTime = "ShortOpt";
elseif SongTime >= 3
SongTime = "IntermediateOpt";
elseif SongTime >= 4
SongTime = "LongOpt";
end
fprintf(' The new song time was entered successfully. \n');
fprintf(" \n");
fprintf(" Enter the song genre. \n");
fprintf(" The three options for the song's genre are Rock, Pop, and Country. \n");
SongGenre = input(" What is the song's genre? ",'s');
while strcmpi(SongGenre,'Rock') ~=1 && strcmpi(SongGenre,'Pop') ~=1 ...
&& strcmpi(SongGenre,'Country') ~=1
fprintf(' You have selected a not existing option \n');
fprintf(' Selected Rock, Pop, or Country. \n');
SongGenre = input(' Selected the correct option ','s');
fprintf(' The new song genre was entered successfully. \n');
end
fprintf(" The genre of the new songs was entered correctly. \n");
fprintf(" \n");
fprintf(" The three options for the song's language are English, Italian, and Romanian. \n");
SongLanguage = input(" What is the language of the song? ", 's');
while strcmpi(SongLanguage,'English') ~=1 && strcmpi(SongLanguage,'Italian') ~=1 ...
&& strcmpi(SongLanguage,'Romanian') ~=1
fprintf(' You have selected a not existing option NERD!!! \n');
fprintf(' Selected English, Italian, or Romanian. \n');
SongLanguage = input(' Selected the correct option ','s');
end
fprintf(' The new song language was entered successfully. \n');
fprintf(" \n");
fprintf(' The song name is %s \n', SongName);
fprintf(' The song singer is %s \n', SongSinger);
fprintf(' The song time is %s \n', SongTime);
fprintf(' The song genre is %s \n', SongGenre);
fprintf(' The song language is %s \n', SongLanguage);
fprintf(' Select Yes if desired to save the changes; if not select No \n');
RepreProcessA = input(' Yes or No ','s' );
while strcmpi(RepreProcessA, 'No') ~= 1 && strcmpi(RepreProcessA, 'Yes') ~= 1
fprintf(' Select Yes or No NERD!!! \n');
RepreProcessA = input(' Selected Yes if desired to save the changes; if not selected No ','s');
end
songcounter = songcounter + 1;
SongData(songcounter,:) = [{SongName}, {SongSinger} {SongTime}, {SongGenre}, {SongLanguage}];
% Use WriteMode = 'append' to add to end of excel file
writecell(SongData(songcounter,:),'Music_Database.xlsx','WriteMode','append')
fprintf(' Song saved succesfully!\n');
% elseif Edit Block
% elseif Delete Block
end
% Have user return to main menu and select Add, Edit or Delete
% again. The user will return to select what they want to do
% again instead of multiple while loops to add, edit, or delete
% multiple.
fprintf( 'Would you like to exit or return to the menu?\n ');
ExitSelection = input( 'Exit or Return? ','s' );
while strcmpi(ExitSelection,'Exit') ~=1 && strcmpi(ExitSelection,'Return') ~=1
fprintf(' You have selected a not existing option \n');
ExitSelection = input(' Selected a valid option ','s');
end
if strcmpi(ExitSelection,"Exit")
fprintf( 'Exiting...\n' );
exitflag = true;
else
fprintf( 'Returning...\n' );
end
end
end
  5 commentaires
Adam Drake
Adam Drake le 14 Mar 2023
Indices allow you to access the data in a vector or matrix. This is very basic, I would recommend doing a MATLAB coding course on YouTube or reviewing documentation in MathWorks help.
Could you please accept my answer. Thanks!
Enrico
Enrico le 14 Mar 2023
Thanks, I think I already accept your answer. Correct me if not.
Thank You

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by