How can i save multiple sets on data from MATLAB App Designer to excel?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
So i have created a call back function for my 'save data' edit text box. So when the user presses the 'save data' button the data from the simulation is saved to an excel file and this is my code below. However if i run multiple simulations and press 'save data' the data is saved on top of the original values and so it only saves one set of data each time.



I wanted to know how to save multiple data sets one after another, underneath one another, every time the user presses 'save data'.
Thanks in advance, Grace
Accepted Answer
Ameer Hamza
on 10 May 2020
Edited: Ameer Hamza
on 10 May 2020
See the 'Range' option of writetable(): https://www.mathworks.com/help/releases/R2020a/matlab/ref/writetable.html#d120e1378251. You can create a property in the app to store the current row. Increment it after each callback and use it to specify the row in excel file.
For example, if the row number is 5, then you can use something like this
writetable(t, 'filename', 'Range', ['A', num2str(5)])
12 Comments
Grace Frost
on 10 May 2020
Thankyou!!
when doing this i get this data:

is there any way of not repeating the value titles?
Ameer Hamza
on 10 May 2020
You can also pass 'WriteVariableNames' property to writetable to avoid writing the variable names.
writetable(t, 'filename', 'Range', ['A', num2str(5)], 'WriteVariableNames', false)
Also, I write five just as an example, you should create a property named row_number in your app and then use it to specify the row number. For example
writetable(t, 'filename', 'Range', ['A', num2str(row_number)], 'WriteVariableNames', false)
row_number = row_number+1;
Grace Frost
on 10 May 2020
Thanks and is it possible to create a while function to keep outputting data to excel, everytime 'save data' is pressed?
Ameer Hamza
on 10 May 2020
Why do you need to use a while-loop? The callback function is called whenever the push button is pressed, so everytime it will add the new line to the excel file.
Grace Frost
on 10 May 2020
oh thanks, I didnt realise it would be re-called everytime. But, when i type in the code above i get this error:

Do you know why i have an error ?
Ameer Hamza
on 10 May 2020
I asked you to define row_number as a property of the app so that it will remain saved after each callback. My code was just an example. To create a property in, switch to code view, go the bar at the top of the app-designer window and create a new property. Name it row_number and assign it a value of 1. And then in the callback function write this
writetable(t, 'filename', 'Range', ['A', num2str(app.row_number)], 'WriteVariableNames', false)
app.row_number = app.row_number+1;
Grace Frost
on 10 May 2020
And what do you mean by 'property'?
Ameer Hamza
on 10 May 2020
See this.

Grace Frost
on 10 May 2020
Thank you so much, its working now. Sorry im new at matlab so trying to get to grips with it.
Ameer Hamza
on 10 May 2020
No problem! I am glad to be of help.
Grace Frost
on 10 May 2020
Hi Ameer, sorry to bother again,
but when i save my data it does save but its saves on row 20 or 21 or 22 or 23 for some reason and not at the top of the excel spreadsheet. This is the code i have, dont think it chnaged any of it. Do you know why?


Ameer Hamza
on 10 May 2020
You have written
.... 'range', ['A2' num2str(.....
replace 'A2' with just 'A'.
More Answers (1)
saleh said bouhliga
on 10 Oct 2020
For example
filename = book1
sheet = sheet1
table name = app.t
writetable(app.t,"Book1.xlsx","Sheet",1);
Categories
Find more on Spreadsheets in Help Center and File Exchange
Tags
See Also
on 10 May 2020
on 10 Oct 2020
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)