Open an excel sheet after clicking a pushbutton in GUI

9 次查看(过去 30 天)
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)

采纳的回答

Shameer Parmar
Shameer Parmar 2016-6-16
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 个评论
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 2017-6-10
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.

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2016-6-14
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 2016-6-15
Use copyfile():
copyfile(existingXLTemplateFileName, newWorkbookFileName);
If you then want to open it up in Excel, and you have Windows, do this:
winopen(newWorkbookFileName);

标签

Community Treasure Hunt

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

Start Hunting!

Translated by