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

1 view (last 30 days)
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?

Accepted Answer

Geoff Hayes
Geoff Hayes on 7 Sep 2015
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 Comments
Alexandra Topciov
Alexandra Topciov on 8 Sep 2015
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 on 14 Sep 2015
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)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!