how to Save Data in Excel Sheet ?

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

Please try this code:
clc
clear
% Your Inputs
NewName = {'Adnan'};
NewValues = [24,18, 3.55];
% Check if you have created an Excel file previously or not
checkforfile=exist(strcat(pwd,'\','ExcelFile.xls'),'file');
if checkforfile==0; % if not create new one
header = {'name', 'Age' 'Rollnum' , 'GPA'};
xlswrite('ExcelFile',header,'Sheetname','A1');
N=0;
else % if yes, count the number of previous inputs
N=size(xlsread('ExcelFile','Sheetname'),1);
end
% add the new values (your input) to the end of Excel file
AA=strcat('A',num2str(N+2));
BB=strcat('B',num2str(N+2));
xlswrite('ExcelFile',NewName,'Sheetname',AA);
xlswrite('ExcelFile',NewValues,'Sheetname',BB);

6 Comments

Thanks This code is perfectly working for me :)
This was a sample scenario . Actual scenario is bit difficult, what if I want to Add another Column which contain String Values ? I tried with these Change but it did not worked. Garbage Values take place.
NewNames = {'Adnan'};
NewValues = ['Ali',22,18, 3.55,4];
% Check if you have created an Excel file previously or not
checkforfile=exist(strcat(pwd,'\','ExcelFile.xls'),'file');
if checkforfile==0; % if not create new one
header = {'Name','Last Name', 'Age' 'Rollnum' , 'GPA','TGPA'};
xlswrite('ExcelFile',header,'Sheetname','A1');
N=0;
else % if yes, count the number of previous inputs
N=size(xlsread('ExcelFile','Sheetname'),1);
end
% add the new values (your input) to the end of Excel file
AA=strcat('A',num2str(N+2));
BB=strcat('B',num2str(N+2));
xlswrite('ExcelFile',NewNames,'Sheetname',AA);
xlswrite('ExcelFile',NewValues,'Sheetname',BB);
Adnan, you need to think carefully about the final format that you would like to see. I mean the number of columns is really important since you don't want to change your format by time. please let me know the final format which you would like to have in Excel. I will edit the code for you.
Amir
Amir on 15 Aug 2014
Edited: Amir on 17 Oct 2014
Another thing which I forgot to say is that delete your excel file from directory after any changes you apply to your code. Then your code will create a new Excel file with the desired columns.
I made the code with the help of your code. and that is working fine. That code i have Posted Below. thank for your help. if u can improve the code then it would be good. Please check the code below.
Here i Made the Code form code given by @Amir
It will create new file if it is not already,
and if file exist then append the row below.
filename='Features.xlsx';
N='Adnan'; a=22; roll=22; gpa=3.55;
fileExist = exist(filename,'file');
if fileExist==0
header = {'Name', 'age ','roll' , 'gpa'};
xlswrite(filename,header);
else
[~,~,input] = xlsread(filename); % Read in your xls file to a cell array (input)
new_data = {N, a,roll , gpa}; % This is a cell array of the new line you want to add
output = cat(1,input,new_data); % Concatinate your new data to the bottom of input
xlswrite(filename,output); % Write to the new excel file.
end
You attached my demo so I'm not really sure whose code you used. If you have to append many lines of data then I can guarantee you 100% that you'd be faster using my ActiveX code than repeatedly calling xlsread and xlswrite for each line you want to append. If you want to append 500 lines of data, then with Amir's code you'll be launching Excel 1000 times, and shutting down Excel 1000 times, versus launching 1 times and shutting down 1 time, so my code would be 1000 times faster. It would actually be faster even if you only appended 2 lines of data.

Sign in to comment.

More Answers (3)

Image Analyst
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

Thank for ur Answer (y).
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.
Adnan Ali
Adnan Ali on 15 Aug 2014
Edited: Adnan Ali on 15 Aug 2014
yes m beginner, please attach a demo if possible. I m having trouble with Amir code while working with strings. and I did not even got your code as well. thanks
See attached demo for using ActiveX to control Excel.

Sign in to comment.

Michal Gajewski
Michal Gajewski on 15 Jan 2016
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
Ashraf Rayed on 12 May 2020
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

Tags

Asked:

on 14 Aug 2014

Answered:

on 12 May 2020

Community Treasure Hunt

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

Start Hunting!