how to Save Data in Excel Sheet ?
17 ビュー (過去 30 日間)
古いコメントを表示
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 "
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145097/image.jpeg)
Thanks in Advance
0 件のコメント
採用された回答
Amir
2014 年 8 月 14 日
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 件のコメント
Image Analyst
2014 年 10 月 17 日
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.
その他の回答 (3 件)
Image Analyst
2014 年 8 月 14 日
編集済み: Image Analyst
2014 年 8 月 14 日
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 件のコメント
Michal Gajewski
2016 年 1 月 15 日
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.
0 件のコメント
Ashraf Rayed
2020 年 5 月 12 日
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?
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!