How to callback and save a plot, as image in guide?

3 views (last 30 days)
I have created a GUI with radio buttons which plots two graphs at a time in the interface. There is also a write results push button which when pushed stores data in excel sheet, along with this I also want to save the plots in the .jpeg format to the same folder. Since this has to be done once the button is pushed , I want a similar command like setappdata to set the plot with some specific name and later in the push button function I can getappdata and store using Imwrite. Please suggest a function to store the plot and then call back in another function.
Attached is the image of GUI

Answers (2)

Image Analyst
Image Analyst on 19 Oct 2018

Yudi
Yudi on 20 Oct 2018

Thank you for your immediate feedback. My real problem is not with exporting plot to png, its how to callback in another function.

As stated I am using multiple radio buttons to plot on same axes one by one code is as follows:

function radiobutton4_Callback(hObject, eventdata, handles)
time=getappdata(0,'time');
P3I=getappdata(0,'p3i');
P3V_G=getappdata(0,'p3v_g');
P3V_dev=getappdata(0,'p3v_dev');
axes(handles.axes8);
plot (time, P3I,'b', time, P3V_G,'g'); 
legend ('Inca', 'Vehicle');
title ('Back Pressure'); xlabel('Time (s)'); ylabel('P3 (mbar)');
image1=getframe(handles.axes8); % Here I wanted to set the plot to a frame
setappdata(hObject,'image1',image1); % Here I wanted to set it for callback. 
axes(handles.perdev);
plot (time, P3V_dev,'r')
title ('Back Pressure Deviation'); xlabel('Time (s)'); ylabel('P3 Deviation (%)');

Now that I have set the graphic I want to save it after pressing push button code is as follows:

function res_Callback(hObject, eventdata, handles)
res=getappdata(0,'res');
set(handles.result,'string','Storing Result . . .');
pause(1);
path=getappdata(0,'path');
image1=getappdata(hObject,'image1'); % Here I want to extract the image
imgpath=fullfile(path,'Figure1.jpg');
export_fig(image1,imgpath); % Here I am storing the image to file path, but its return error that argument is empty
file=fullfile(path,'Mass Correlation Result.xlsx');
xlswrite(file, res);
set(handles.result,'string','Results Saved');

It is not a complex problem but I am new to matalab, your help will be much appreciated.

  1 Comment
Image Analyst
Image Analyst on 20 Oct 2018
It could be that your using "path" as the name of your variable is causing problems. path is a built-in reserved variable that you should never overwrite/destroy. Call it "folder" and try again.
I also don't see any need for pause(1).
Also, you can use the more convenient OOP method of setting properties instead of using the old fashioned get() and set():
handles.result.String = 'Results Saved';

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!