How to make a simple gui to plot a scatter graph?

13 views (last 30 days)
Hello everybody! I'm required to create a gui (with a graph window and a push button) in Matlab that would import data from excel and plot it on a graph. So, for example, say there are 2 columns, 1 with amount of km's the car has done and the other one is with fuel left. I need to import that data to the gui program and plot a line graph of km's against fuel left. I've been trying to find guides on making scatter graphs in gui for some time now,but I am reall struggling with it. Could someone please help me with a code or maybe knows a similar guide. Also,I should probably say that i've been using matlab for 5 days only. I've managed to familiarise myself with basic GUI programming,but this seems to be far more complicated. Thank you!!!

Accepted Answer

Conrad
Conrad on 11 Jul 2012
Hi Kokalz, the code below should get you started:
function DemoGUI()
% Initialise variables:
excelFileName = ''; % Add the Excel file
% name (full path) here.
excelSheetName = ''; % Add the name of the sheet
% that contains the data here.
l = figure; % Create a new figure.
buttonImport = uicontrol( 'units', 'normalized',...
'style', 'pushbutton',...% Style of control.
'string', 'Import',...% Display name.
'position', [0.05 0.05 0.2 0.05],...% Position.
'callback', @Import);% Callback function.
globalAxes = axes( 'units','normalized',...
'position', [0.2 0.2 0.7 0.7]);
function Import(object,src,event)
% Uncomment the next two statements to use Excel data.
% [num txt] = xlsread(excelFileName,excelPathName);
% xData = num(:,1); yData = num(:,2);
% Use dummy data for the moment.
xData = rand(10,1); yData = rand(10,1);
axes(globalAxes);
p = plot(xData,yData,'ro'); % Plot data.
end
end
Please note that this is the bare minimum of code. You could for example launch another GUI when the user clicks on the import button to enable the user to navigate to the Excel file (see the File Exchange).
You could also design your GUI using GUIDE (GUI Builder).

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output 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!