Open an excel sheet after clicking a pushbutton in GUI

6 vues (au cours des 30 derniers jours)
Michel
Michel le 14 Juin 2016
Commenté : Image Analyst le 10 Juin 2017
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)

Réponse acceptée

Shameer Parmar
Shameer Parmar le 16 Juin 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 commentaires
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 le 10 Juin 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.

Connectez-vous pour commenter.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 14 Juin 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.
  2 commentaires
Michel
Michel le 14 Juin 2016
I'm afraid i need a more explicit answer. thanks
Walter Roberson
Walter Roberson le 15 Juin 2016
I'm afraid I need a more explicit question. thanks

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 15 Juin 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!

Translated by