How can I use xlswrite to get data from GUI after I push a button?

조회 수: 1 (최근 30일)
I need to get some data from GUI and write it in an excel-file but only after i click a button, and I don't want to overwrite the data after each button press but make it go to the next line. Can anyone help me?

채택된 답변

Geoff Hayes
Geoff Hayes 2015년 9월 7일
Alexandra - if you don't want to overwrite the data that has been previously written to the Excel spreadsheet, then just specify the location (row) of where you want to write the data to. For example,
function pushbutton1_Callback(hObject, eventdata, handles)
% get data from GUI (assume row)
rowData = [1 2 3 4 5];
% get row to write to
row = 1;
if isfield(handles,'row')
row = handles.row;
end
% write the data to file to row Ax
xlswrite('myExcelFile.xls', rowData, ['A' num2str(row)]);
% update handles struct with the new row
handles.row = row + 1;
guidata(handles);
Note how we use the range parameter of xlswrite to determine which row we write the new data to: we write the data starting in the first column (A) and concatenate with that the integer row. We save to the handles structure the new row to use when we want to write data to this file upon the next press of the button (using guidata).
The isfield check is there to ensure we don't try to access a field of handles that doesn't exist.
Try the above and see what happens!
  댓글 수: 2
Alexandra Topciov
Alexandra Topciov 2015년 9월 8일
Thank you very much. I just needed to change a few thing, and in case someone is having the same problem as me, here they are: 1. I had to make the data from GUI as cells instead of matrix, otherwise it wouldn't align the data right (ex: if i had x=10 it would write 1 in A1 and 0 in B1), so '{}' instead of '[]' 2. I had to specific the sheet in excel otherwise it would have just made new sheets named A1,A2..A(n)
Alexandra Topciov
Alexandra Topciov 2015년 9월 14일
Is it possible to make some kind of loop so it would start at A1 and write on the first empty row everytime i run the program? (e.g: if i write in A1, A2, A3 and then erase what is written in A2, the next time I press the button it writes in A4, I want it to write in the empty cell A2)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by