Open an excel sheet after clicking a pushbutton in GUI

7 views (last 30 days)
I would like to create a GUI so whenever I click the push-button (I called it OK), matlab creates an excel sheet based on an already prepared excel template. Thank you for your help.
% --- Executes on button press in OK_.
function OK__Callback(hObject, eventdata, handles)
% hObject handle to OK_ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

Accepted Answer

Shameer Parmar
Shameer Parmar on 16 Jun 2016
Hello Michel,
Consider you have GUI " Test.m" and " Test.fig" files, along with your template " ABC.xlsx" file.
Keep all files in current directory of matlab. Now, for your GUI, there is pushbutton called ' OK'.
Consider, the template Excel file have three column ' Sr_No', ' Name', ' Age', which you want to update when you click on ' OK' push button.
so you need to update the following code into callback of pushbutton ' OK':
% --- Executes on button press in OK_.
function OK__Callback(hObject, eventdata, handles)
% hObject handle to OK_ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Sr_No = {'1';'2';'3';'4';'5'};
Name = {'A';'B';'C';'D';'E'};
Age = {'10';'11';'12';'13';'15'};
final_Data = [Sr_No,Name,Age];
xlswrite('ABC.xlsx',final_Data,'Sheet1','A2');
winopen('ABC.xlsx');
  2 Comments
Koushik Bhadravathi Chandrashekhar
Hello Shameer, I found your answer useful for my problem, but not completely. In my situation, my excel sheet opens from a python script after running it in Ansys CAE software. Now, i want to control excel parameter values from matlab GUI. By following your steps, a new excel sheet opens and updates the values. But, i want matlab GUI to update already opened excel sheet instead of updating in a new one because the already opened excel sheet does the important things in Ansys as written in my python script.
please help me in achieving this!
Regards, Koushik
Image Analyst
Image Analyst on 10 Jun 2017
Call actxGetRunningServer().
h = actxGetRunningServer('progid') gets a reference to a running instance of the OLE Automation server. progid is the programmatic identifier of the Automation server object and h is the handle to the default interface of the server object.
Then control Excel. See attached example.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 14 Jun 2016
Fetch the template, get() any necessary properties from the uicontrols and use the values to update the copy of the template, then xlswrite() the appropriate file.

Image Analyst
Image Analyst on 15 Jun 2016
Use copyfile():
copyfile(existingXLTemplateFileName, newWorkbookFileName);
If you then want to open it up in Excel, and you have Windows, do this:
winopen(newWorkbookFileName);

Tags

Community Treasure Hunt

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

Start Hunting!