MATLAB: Can i save excel files in app designer

excelMATLABmatlab appdesigner

Hello,
In my app i have push button and want the to save a 8 excel files with in the app. Reason for this is as the user makes his/her desired selection prior to using this push button (export) based on the users selection i want one the template(excel file) to be choosen which ever is assigned to the condition and selection made by the user. Please let me know if i could save excel files within an app?
Thanks
Sai

Best Answer

You can save any variable of your App to excel, using either xlswrite or writetable.
%... in the app class
function SaveToExcel(app) %possibly as a callback to some button
folder = app.ExcelFolder; %for example

filename = app.ExcelFilename; %for example
xlswrite(fullfile(folder, filename), app.somevariable);
end
If you want to save several variables to excel, you can either group them together in a table or cell array (preferred) or write them in sequence through multiple calls to xlswrite/writetable (slower).
Similarly, you can load the content of an excel file into any variable of your app. Note that if you're using something like a uilistbox to let the user select one of several templates, you would use neither if nor switch to actually load the file. Indexing is a lot simpler and flexible.
%creation of the uilistbox in the CreateComponent method or the StartupFcn callback
app.listboxTemplate = uilistbox(app.Figure, ...
'Items', {'Template for aaa', 'Template for bbb'}, ... stuff to display
'ItemsData', {'template1.xlsx', 'template2.xlsx'}); % actual file to load
%in the function that loads the template. Possibly a callback
function LoadTemplate(app)
folder = app.ExcelFolder;
if ~isempty(app.listboxTemplate.Value)
app.ExcelTemplate = xlsread(fullfile(folder, app.listboxTemplateValue)); %the list box value is the content of ItemsData for the selected entry
else
uialert(app.Figure, 'No template selected');
end
end