output appdesigner data collected with button press to next row in excel

1 view (last 30 days)
I have code that writes out data (string) to an excel spreadsheet. When the user clicks <Submit>, the information in the text area is output to an excel file ("ArticleRequest.xlsx"). This part works well in Matlab app designer 2019b version 5. When I run the app a second time, the data should go to the next row in excel but instead it overwrites the information in the first cell. How can I fix this? I have looked everywhere on this site, on Stack Overflow, in Matlab Help and no one seems to have a code that works in app designer. The code that works is below. The last 4 rows are directions to the user after the data is output to the excel file. Any helpful suggestions are appreciated.
% Button pushed function: SubmitButton
function SubmitButtonPushed(app, event)
filename = 'ArticleRequest.xlsx'; %file
M = {app.TextArea_Email.Value}; %data
writecell(M,filename,'Sheet','EmailReq'); %write data to cell in table
app.TextArea_Email.Value = 'Press <Next> to continue.';
app.SubmitButton.Visible = 'off';
uiconfirm(app.UIFigure,{'The file has been created.','Click OK'},'File Creation Confirmation','Icon','Success');
app.NextButton_Email.Enable = 'on';
end

Accepted Answer

frankenberry
frankenberry on 4 Apr 2020
This Works!
% Button pushed function: SubmitButton
function SubmitButtonPushed(app, event)
filename = 'ArticleRequest.xlsx'; %file
newEmail = {app.TextArea_Email.Value}; %data
Emails = readcell('ArticleRequest.xlsx','Range','Var1'); % read in emails
% append new row to emails array
Emails = [Emails; newEmail]; % append new email to list of emails
writecell(Emails,filename); % write it to file
app.TextArea_Email.Value = 'Press <Next> to continue.';
app.SubmitButton.Visible = 'off';
uiconfirm(app.UIFigure,{'The file has been created.','Click OK'},'File Creation Confirmation','Icon','Success');
app.NextButton_Email.Enable = 'on';
end

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!