how to Save Data in Excel Sheet ?
Show older comments
I want to save my data in form of table in Excel Sheet. it should look like the Picture given below. Every time when I will execute my file classData.m , A row will be added below. like i want to add next row as
"Ahmad 21 44 3.53 "

Thanks in Advance
Accepted Answer
More Answers (3)
Image Analyst
on 14 Aug 2014
Edited: Image Analyst
on 14 Aug 2014
Just call xlswrite() with the new row of data. You need to keep track of what row to stuff your data into. For example, maybe in your main calling routine, you have a loop where you call classData with your data. Somehow you get new data with each iteration, then:
newData = "Ahmad 21 44 3.53 " % Whatever this happens to be this time...
rowToWrite = 5; % or whatever.
cellReference = sprintf('A%d', rowToWrite);
classData(newData, cellReference);
rowToWrite = rowToWrite + 1;
4 Comments
Adnan Ali
on 14 Aug 2014
Image Analyst
on 14 Aug 2014
If you can keep track of the row yourself, like in my answer, then you'll save a lot of time reading it in as in Amir's answer. If you can't do that, like the workbook already exists with some unknown number of rows before your program starts, then you can call xlsread to get the current number of rows. But once you know that, you should keep track of it like I suggested because calling xlsread entails launching Excel, hauling the data over into MATLAB, and then shutting down Excel. This takes a lot of time and in not something you'd want to do in a loop of dozens or hundreds of times. Even better would be to use ActiveX but that's a little advanced and if you're a beginner it might be a little difficult to learn. I can attach a demo if you want.
Image Analyst
on 16 Aug 2014
See attached demo for using ActiveX to control Excel.
Michal Gajewski
on 15 Jan 2016
0 votes
There is also another easier way (Only with one function 'length'): Let's say that you have your headers and 3 lines of data (like in your example).
You have to read your file:
YourXlsxFile=xlsread(xlsxfilename);
find first empty row:
RowEmptyNumber=length(YourXlsxFile(:,1))+1; - in this case this is last element+1;
and just save data in new empty row:
A = {Ahmad, 21, 44, 3.53};
sheet = 1;
xlRange=strcat('A',num2str(RowEmptyNumber)); % this function is for create cell 'A4' as starting point in your case.
xlswrite(xlsxfilename,A,sheet,xlRange); % save data to new row
I hope this is useful solution.
Ashraf Rayed
on 12 May 2020
0 votes
Sir i have question here. I have to detect the area of leaves in some page and i want save those data of area in excel sheet. Means when i check one picture it is saved, then after that when i will check another pic it will not replace the data rather than it will also be saved with the previous one in rcolumn wise in a excel sheet. can you please give me some type of that code?
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!