It is working, but when I uncomment the section that is comment inted or adding one song, it adds all of them.

8 views (last 30 days)
It is working, but when I uncomment the section that is comment inted or adding one song, it adds all of them. example
if ihave 20 songs and i add one it will copy all 20 plus the one added and then they became 41 insted of 21.
code
fprintf(' Let''s get started! \n');
% Define variables.
SongFound = 0;
exitflag = false;
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')
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
NewData = [{SongName}, {SongSinger} {SongTime}, {SongGenre}, {SongLanguage}];
writecell(NewData,'Music_Database.xlsx','WriteMode','append')
fprintf(' Song saved succesfully!\n');
end
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

Answers (1)

Samayochita
Samayochita on 24 Feb 2025
Edited: Samayochita on 24 Feb 2025
Hi Enrico,
I understand that you are trying to append one song, instead your code is appending the entire dataset (along with the new entry). I can see a couple of issues in your code that could be causing this:
  • Try changing NewData list from
NewData = [{SongName}, {SongSinger} {SongTime}, {SongGenre}, {SongLanguage}];
to
NewData = {SongName, SongSinger, SongTime, SongGenre, SongLanguage};
The incorrect placement of {} might be causing MATLAB to interpret the entire dataset as a single entity instead of separate entries.
  • Another issue could be that the writecell function always appends data when WriteMode option is set to 'append'. If something went wrong in a previous run, it could be duplicating the entire file contents. To prevent this, you should first read the existing data from “Music_Database.xlsx”, append only the new entry and then write back to the file. I have written an example code section for your reference:
if isfile('Music_Database.xlsx')
ExistingData = readcell('Music_Database.xlsx');
else
ExistingData = {}; % Create empty data if file doesn't exist
end
NewData = {SongName, SongSinger, SongTime, SongGenre, SongLanguage};
UpdatedData = [ExistingData; NewData]; % Append only the new row
writecell(UpdatedData, 'Music_Database.xlsx');
Hope this helps!

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!