how to use buitin functions like fcm in gui?

1 view (last 30 days)
I am trying to implment a GUI for a project. I have to use the fcm builtin function . How to call this function from the gui from the callback and display back the results and
as well plot the image of the dataset.

Accepted Answer

Rik
Rik on 24 Feb 2020
Edited: Rik on 24 Feb 2020
A GUI in Matlab is nothing special. You need to have working code first, and then put that into a GUI. If you have trouble with the first step, the second not relevant yet.
If you want advice about how to make a GUI, read this thread.
Your tags sugest that you're planning to use GUIDE. Especially if you're creating a GUI from scratch I would strongly advise you to not use GUIDE. It is slated to be discontinued and it can be complex to create anything that isn't simple.
  6 Comments
Maddie
Maddie on 25 Feb 2020
This is the ui of the project:
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
BrowseButton matlab.ui.control.Button
ClusterEditFieldLabel matlab.ui.control.Label
ClusterField matlab.ui.control.NumericEditField
Button matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
EditFieldLabel matlab.ui.control.Label
EditField matlab.ui.control.EditField
end
properties (Access = private)
data % Description
cluster % Description
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: BrowseButton
function BrowseButtonPushed(app, event)
[filename, pathname] = uigetfile({'*.txt'},'Please select a file to upload.');
fullpath = strcat(pathname,filename);
app.data=load(fullpath);
end
% Button pushed function: Button
function ButtonPushed(app, event)
forkmean(app.data,app.cluster);
end
% Value changed function: ClusterField
function ClusterFieldValueChanged(app, event)
value = app.ClusterField.Value;
app.cluster=value;
end
% Value changed function: EditField
function EditFieldValueChanged(app, event)
value = app.EditField.Value;
app.EditField.Value= app.data;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create BrowseButton
app.BrowseButton = uibutton(app.UIFigure, 'push');
app.BrowseButton.ButtonPushedFcn = createCallbackFcn(app, @BrowseButtonPushed, true);
app.BrowseButton.Position = [501 424 100 22];
app.BrowseButton.Text = 'Browse';
% Create ClusterEditFieldLabel
app.ClusterEditFieldLabel = uilabel(app.UIFigure);
app.ClusterEditFieldLabel.HorizontalAlignment = 'right';
app.ClusterEditFieldLabel.VerticalAlignment = 'top';
app.ClusterEditFieldLabel.Position = [435 362 54 15];
app.ClusterEditFieldLabel.Text = 'Cluster #';
% Create ClusterField
app.ClusterField = uieditfield(app.UIFigure, 'numeric');
app.ClusterField.ValueChangedFcn = createCallbackFcn(app, @ClusterFieldValueChanged, true);
app.ClusterField.Position = [504 358 100 22];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [502 272 100 22];
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
app.UIAxes.GridAlpha = 0.15;
app.UIAxes.MinorGridAlpha = 0.25;
app.UIAxes.Position = [26 102 407 350];
% Create EditFieldLabel
app.EditFieldLabel = uilabel(app.UIFigure);
app.EditFieldLabel.HorizontalAlignment = 'right';
app.EditFieldLabel.VerticalAlignment = 'top';
app.EditFieldLabel.Position = [411 213 56 15];
app.EditFieldLabel.Text = 'Edit Field';
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'text');
app.EditField.ValueChangedFcn = createCallbackFcn(app, @EditFieldValueChanged, true);
app.EditField.Position = [482 209 100 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
This is a function that I called form the callback of the ButtonPushed I want to plot this graph generated from this function in the axes.
function forkmean(data,K)
[idx,C] = kmeans(data,K);
figure,
gscatter(data(:,1),data(:,2),idx);
hold on
for i = 1:5
scatter(C(i,1),C(i,2) ,96, 'black','filled');
end
legend({'Cluster 1', 'Cluster 2', 'Cluster 3', 'Cluster 4', 'Cluster 5'})
xlabel('Annual Income');
ylabel('Spending Scores')
hold off
end
Rik
Rik on 25 Feb 2020
If you want to plot something in a specific axes, you should provide its handle as the parent in your call to scatter, instead of letting the forkmean function create a new figure.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop uifigure-Based Apps 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!