how to use the generated code from classification learner app in MATLAB to predict new data? The Next CEO of Stack OverflowWhy does MATLAB complain it “forbids the use of the same name … as both a function and a variable”?Reading an audio file through Matlab GUIHow to erase a filled 2D polygon, Projectile Animation?MATLAB GUI error when filling textboxes with data from fileMatlab GUI plot from function not workingTMG matlab tool errorMatlab - two active GUIsmatlab Error using fevalcheckbox object IF statement does not recognize the variables in MATLAB GUIDEInserting data into MSSQL from Matlab

Does it take more energy to get to Venus or to Mars?

Is it my responsibility to learn a new technology in my own time my employer wants to implement?

Why do airplanes bank sharply to the right after air-to-air refueling?

Would a galaxy be visible from outside, but nearby?

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

What connection does MS Office have to Netscape Navigator?

Do I need to enable Dev Hub in my PROD Org?

How do I go from 300 unfinished/half written blog posts, to published posts?

What was the first Unix version to run on a microcomputer?

What does "Its cash flow is deeply negative" mean?

Non-deterministic sum of floats

Return the Closest Prime Number

Sending manuscript to multiple publishers

Make solar eclipses exceedingly rare, but still have new moons

How to solve a differential equation with a term to a power?

How does the mv command work with external drives?

Anatomically Correct Strange Women In Ponds Distributing Swords

Why does the UK parliament need a vote on the political declaration?

Can we say or write : "No, it'sn't"?

Can I equip Skullclamp on a creature I am sacrificing?

Why do remote companies require working in the US?

Rotate a column

calculus parametric curve length

How to avoid supervisors with prejudiced views?



how to use the generated code from classification learner app in MATLAB to predict new data?



The Next CEO of Stack OverflowWhy does MATLAB complain it “forbids the use of the same name … as both a function and a variable”?Reading an audio file through Matlab GUIHow to erase a filled 2D polygon, Projectile Animation?MATLAB GUI error when filling textboxes with data from fileMatlab GUI plot from function not workingTMG matlab tool errorMatlab - two active GUIsmatlab Error using fevalcheckbox object IF statement does not recognize the variables in MATLAB GUIDEInserting data into MSSQL from Matlab










0















i having trouble to call the function from the generated code and i want to display the output in command window.



function varargout = ipwebcamGUI(varargin)
% IPWEBCAMGUI MATLAB code for ipwebcamGUI.fig
% IPWEBCAMGUI, by itself, creates a new IPWEBCAMGUI or raises the existing
% singleton*.
%
% H = IPWEBCAMGUI returns the handle to a new IPWEBCAMGUI or the handle to
% the existing singleton*.
%
% IPWEBCAMGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in IPWEBCAMGUI.M with the given input arguments.
%
% IPWEBCAMGUI('Property','Value',...) creates a new IPWEBCAMGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ipwebcamGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ipwebcamGUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ipwebcamGUI

% Last Modified by GUIDE v2.5 07-Mar-2019 22:03:50

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ipwebcamGUI_OpeningFcn, ...
'gui_OutputFcn', @ipwebcamGUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin1)
gui_State.gui_Callback = str2func(varargin1);
end

if nargout
[varargout1:nargout] = gui_mainfcn(gui_State, varargin:);
else
gui_mainfcn(gui_State, varargin:);
end
% End initialization code - DO NOT EDIT


% --- Executes just before ipwebcamGUI is made visible.
function ipwebcamGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ipwebcamGUI (see VARARGIN)

% Choose default command line output for ipwebcamGUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ipwebcamGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ipwebcamGUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout1 = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)


axes(handles.axes1);
im=imread('c1.jpg');
k=rgb2gray(im);

% Threshold image - global threshold
BW = imbinarize(k);

% Invert mask
BW = imcomplement(BW);

%clear border
BW = imclearborder(BW);

% Create masked image.
maskedImage = k;
maskedImage(~BW) = 0;

%count the white pixel in image
whitepix = sum(BW(:));

axis off;
imshow(BW);
clear


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

T = trainClassifier2(trainingData);
yfit = trainedClassifier.predictFcn(T);
fprintf('result %d n',yfit );
% set(handles.text2,'String',T);
% drawnow();


this file named ipwebcamGUI.m
and the second trainClassifier2.m



function [trainedClassifier, validationAccuracy] = trainClassifier2(trainingData)
% trainClassifier(trainingData)
% returns a trained classifier and its accuracy.
% This code recreates the classification model trained in
% Classification Learner app.
%
% Input:
% trainingData: the training data of same data type as imported
% in the app (table or matrix).
%
% Output:
% trainedClassifier: a struct containing the trained classifier.
% The struct contains various fields with information about the
% trained classifier.
%
% trainedClassifier.predictFcn: a function to make predictions
% on new data. It takes an input of the same form as this training
% code (table or matrix) and returns predictions for the response.
% If you supply a matrix, include only the predictors columns (or
% rows).
%
% validationAccuracy: a double containing the accuracy in
% percent. In the app, the History list displays this
% overall accuracy score for each model.
%
% Use the code to train the model with new data.
% To retrain your classifier, call the function from the command line
% with your original data or new data as the input argument trainingData.
%
% For example, to retrain a classifier trained with the original data set
% T, enter:
% [trainedClassifier, validationAccuracy] = trainClassifier(T)
%
% To make predictions with the returned 'trainedClassifier' on new data T,
% use
% yfit = trainedClassifier.predictFcn(T)
%
% To automate training the same classifier with new data, or to learn how
% to programmatically train classifiers, examine the generated code.

% Auto-generated by MATLAB on 03-Mar-2019 14:35:31


% Extract predictors and response
% This code processes the data into the right shape for training the
% classifier.
inputTable = trainingData;
predictorNames = 'VarName1';
predictors = inputTable(:, predictorNames);
response = inputTable.A;
isCategoricalPredictor = [false];

% Train a classifier
% This code specifies all the classifier options and trains the classifier.
classificationTree = fitctree(...
predictors, ...
response, ...
'SplitCriterion', 'gdi', ...
'MaxNumSplits', 100, ...
'Surrogate', 'off', ...
'ClassNames', 'A'; 'B'; 'C');

% Create the result struct with predict function
predictorExtractionFcn = @(t) t(:, predictorNames);
treePredictFcn = @(x) predict(classificationTree, x);
trainedClassifier.predictFcn = @(x) treePredictFcn(predictorExtractionFcn(x));

% Add additional fields to the result struct
trainedClassifier.RequiredVariables = 'VarName1';
trainedClassifier.ClassificationTree = classificationTree;
trainedClassifier.About = 'This struct is a trained classifier exported from Classification Learner R2016a.';
trainedClassifier.HowToPredict = sprintf('To make predictions on a new table, T, use: n yfit = c.predictFcn(T) nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedClassifier''. n nThe table, T, must contain the variables returned by: n c.RequiredVariables nVariable formats (e.g. matrix/vector, datatype) must match the original training data. nAdditional variables are ignored. n nFor more information, see <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appclassification_exportmodeltoworkspace'')">How to predict using an exported model</a>.');

% Extract predictors and response
% This code processes the data into the right shape for training the
% classifier.
inputTable = trainingData;
predictorNames = 'VarName1';
predictors = inputTable(:, predictorNames);
response = inputTable.A;
isCategoricalPredictor = [false];

% Perform cross-validation
partitionedModel = crossval(trainedClassifier.ClassificationTree, 'KFold', 5);

% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');

% Compute validation predictions and scores
[validationPredictions, validationScores] = kfoldPredict(partitionedModel);


the program counts the white pixel in the image.And the "whitepix" variable would be the expected input..the output will be A,B,C..



this part is where i am having a trouble..i want to display the output when button is clicked.im new to this please help me.



function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

T = trainClassifier2(trainingData);
yfit = trainedClassifier.predictFcn(T);
fprintf('result %d n',yfit );









share|improve this question


























    0















    i having trouble to call the function from the generated code and i want to display the output in command window.



    function varargout = ipwebcamGUI(varargin)
    % IPWEBCAMGUI MATLAB code for ipwebcamGUI.fig
    % IPWEBCAMGUI, by itself, creates a new IPWEBCAMGUI or raises the existing
    % singleton*.
    %
    % H = IPWEBCAMGUI returns the handle to a new IPWEBCAMGUI or the handle to
    % the existing singleton*.
    %
    % IPWEBCAMGUI('CALLBACK',hObject,eventData,handles,...) calls the local
    % function named CALLBACK in IPWEBCAMGUI.M with the given input arguments.
    %
    % IPWEBCAMGUI('Property','Value',...) creates a new IPWEBCAMGUI or raises the
    % existing singleton*. Starting from the left, property value pairs are
    % applied to the GUI before ipwebcamGUI_OpeningFcn gets called. An
    % unrecognized property name or invalid value makes property application
    % stop. All inputs are passed to ipwebcamGUI_OpeningFcn via varargin.
    %
    % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
    % instance to run (singleton)".
    %
    % See also: GUIDE, GUIDATA, GUIHANDLES

    % Edit the above text to modify the response to help ipwebcamGUI

    % Last Modified by GUIDE v2.5 07-Mar-2019 22:03:50

    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name', mfilename, ...
    'gui_Singleton', gui_Singleton, ...
    'gui_OpeningFcn', @ipwebcamGUI_OpeningFcn, ...
    'gui_OutputFcn', @ipwebcamGUI_OutputFcn, ...
    'gui_LayoutFcn', [] , ...
    'gui_Callback', []);
    if nargin && ischar(varargin1)
    gui_State.gui_Callback = str2func(varargin1);
    end

    if nargout
    [varargout1:nargout] = gui_mainfcn(gui_State, varargin:);
    else
    gui_mainfcn(gui_State, varargin:);
    end
    % End initialization code - DO NOT EDIT


    % --- Executes just before ipwebcamGUI is made visible.
    function ipwebcamGUI_OpeningFcn(hObject, eventdata, handles, varargin)
    % This function has no output args, see OutputFcn.
    % hObject handle to figure
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)
    % varargin command line arguments to ipwebcamGUI (see VARARGIN)

    % Choose default command line output for ipwebcamGUI
    handles.output = hObject;

    % Update handles structure
    guidata(hObject, handles);

    % UIWAIT makes ipwebcamGUI wait for user response (see UIRESUME)
    % uiwait(handles.figure1);


    % --- Outputs from this function are returned to the command line.
    function varargout = ipwebcamGUI_OutputFcn(hObject, eventdata, handles)
    % varargout cell array for returning output args (see VARARGOUT);
    % hObject handle to figure
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)

    % Get default command line output from handles structure
    varargout1 = handles.output;


    % --- Executes on button press in pushbutton1.
    function pushbutton1_Callback(hObject, eventdata, handles)
    % hObject handle to pushbutton1 (see GCBO)
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)


    axes(handles.axes1);
    im=imread('c1.jpg');
    k=rgb2gray(im);

    % Threshold image - global threshold
    BW = imbinarize(k);

    % Invert mask
    BW = imcomplement(BW);

    %clear border
    BW = imclearborder(BW);

    % Create masked image.
    maskedImage = k;
    maskedImage(~BW) = 0;

    %count the white pixel in image
    whitepix = sum(BW(:));

    axis off;
    imshow(BW);
    clear


    % --- Executes on button press in pushbutton3.
    function pushbutton3_Callback(hObject, eventdata, handles)
    % hObject handle to pushbutton3 (see GCBO)
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)

    T = trainClassifier2(trainingData);
    yfit = trainedClassifier.predictFcn(T);
    fprintf('result %d n',yfit );
    % set(handles.text2,'String',T);
    % drawnow();


    this file named ipwebcamGUI.m
    and the second trainClassifier2.m



    function [trainedClassifier, validationAccuracy] = trainClassifier2(trainingData)
    % trainClassifier(trainingData)
    % returns a trained classifier and its accuracy.
    % This code recreates the classification model trained in
    % Classification Learner app.
    %
    % Input:
    % trainingData: the training data of same data type as imported
    % in the app (table or matrix).
    %
    % Output:
    % trainedClassifier: a struct containing the trained classifier.
    % The struct contains various fields with information about the
    % trained classifier.
    %
    % trainedClassifier.predictFcn: a function to make predictions
    % on new data. It takes an input of the same form as this training
    % code (table or matrix) and returns predictions for the response.
    % If you supply a matrix, include only the predictors columns (or
    % rows).
    %
    % validationAccuracy: a double containing the accuracy in
    % percent. In the app, the History list displays this
    % overall accuracy score for each model.
    %
    % Use the code to train the model with new data.
    % To retrain your classifier, call the function from the command line
    % with your original data or new data as the input argument trainingData.
    %
    % For example, to retrain a classifier trained with the original data set
    % T, enter:
    % [trainedClassifier, validationAccuracy] = trainClassifier(T)
    %
    % To make predictions with the returned 'trainedClassifier' on new data T,
    % use
    % yfit = trainedClassifier.predictFcn(T)
    %
    % To automate training the same classifier with new data, or to learn how
    % to programmatically train classifiers, examine the generated code.

    % Auto-generated by MATLAB on 03-Mar-2019 14:35:31


    % Extract predictors and response
    % This code processes the data into the right shape for training the
    % classifier.
    inputTable = trainingData;
    predictorNames = 'VarName1';
    predictors = inputTable(:, predictorNames);
    response = inputTable.A;
    isCategoricalPredictor = [false];

    % Train a classifier
    % This code specifies all the classifier options and trains the classifier.
    classificationTree = fitctree(...
    predictors, ...
    response, ...
    'SplitCriterion', 'gdi', ...
    'MaxNumSplits', 100, ...
    'Surrogate', 'off', ...
    'ClassNames', 'A'; 'B'; 'C');

    % Create the result struct with predict function
    predictorExtractionFcn = @(t) t(:, predictorNames);
    treePredictFcn = @(x) predict(classificationTree, x);
    trainedClassifier.predictFcn = @(x) treePredictFcn(predictorExtractionFcn(x));

    % Add additional fields to the result struct
    trainedClassifier.RequiredVariables = 'VarName1';
    trainedClassifier.ClassificationTree = classificationTree;
    trainedClassifier.About = 'This struct is a trained classifier exported from Classification Learner R2016a.';
    trainedClassifier.HowToPredict = sprintf('To make predictions on a new table, T, use: n yfit = c.predictFcn(T) nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedClassifier''. n nThe table, T, must contain the variables returned by: n c.RequiredVariables nVariable formats (e.g. matrix/vector, datatype) must match the original training data. nAdditional variables are ignored. n nFor more information, see <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appclassification_exportmodeltoworkspace'')">How to predict using an exported model</a>.');

    % Extract predictors and response
    % This code processes the data into the right shape for training the
    % classifier.
    inputTable = trainingData;
    predictorNames = 'VarName1';
    predictors = inputTable(:, predictorNames);
    response = inputTable.A;
    isCategoricalPredictor = [false];

    % Perform cross-validation
    partitionedModel = crossval(trainedClassifier.ClassificationTree, 'KFold', 5);

    % Compute validation accuracy
    validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');

    % Compute validation predictions and scores
    [validationPredictions, validationScores] = kfoldPredict(partitionedModel);


    the program counts the white pixel in the image.And the "whitepix" variable would be the expected input..the output will be A,B,C..



    this part is where i am having a trouble..i want to display the output when button is clicked.im new to this please help me.



    function pushbutton3_Callback(hObject, eventdata, handles)
    % hObject handle to pushbutton3 (see GCBO)
    % eventdata reserved - to be defined in a future version of MATLAB
    % handles structure with handles and user data (see GUIDATA)

    T = trainClassifier2(trainingData);
    yfit = trainedClassifier.predictFcn(T);
    fprintf('result %d n',yfit );









    share|improve this question
























      0












      0








      0








      i having trouble to call the function from the generated code and i want to display the output in command window.



      function varargout = ipwebcamGUI(varargin)
      % IPWEBCAMGUI MATLAB code for ipwebcamGUI.fig
      % IPWEBCAMGUI, by itself, creates a new IPWEBCAMGUI or raises the existing
      % singleton*.
      %
      % H = IPWEBCAMGUI returns the handle to a new IPWEBCAMGUI or the handle to
      % the existing singleton*.
      %
      % IPWEBCAMGUI('CALLBACK',hObject,eventData,handles,...) calls the local
      % function named CALLBACK in IPWEBCAMGUI.M with the given input arguments.
      %
      % IPWEBCAMGUI('Property','Value',...) creates a new IPWEBCAMGUI or raises the
      % existing singleton*. Starting from the left, property value pairs are
      % applied to the GUI before ipwebcamGUI_OpeningFcn gets called. An
      % unrecognized property name or invalid value makes property application
      % stop. All inputs are passed to ipwebcamGUI_OpeningFcn via varargin.
      %
      % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
      % instance to run (singleton)".
      %
      % See also: GUIDE, GUIDATA, GUIHANDLES

      % Edit the above text to modify the response to help ipwebcamGUI

      % Last Modified by GUIDE v2.5 07-Mar-2019 22:03:50

      % Begin initialization code - DO NOT EDIT
      gui_Singleton = 1;
      gui_State = struct('gui_Name', mfilename, ...
      'gui_Singleton', gui_Singleton, ...
      'gui_OpeningFcn', @ipwebcamGUI_OpeningFcn, ...
      'gui_OutputFcn', @ipwebcamGUI_OutputFcn, ...
      'gui_LayoutFcn', [] , ...
      'gui_Callback', []);
      if nargin && ischar(varargin1)
      gui_State.gui_Callback = str2func(varargin1);
      end

      if nargout
      [varargout1:nargout] = gui_mainfcn(gui_State, varargin:);
      else
      gui_mainfcn(gui_State, varargin:);
      end
      % End initialization code - DO NOT EDIT


      % --- Executes just before ipwebcamGUI is made visible.
      function ipwebcamGUI_OpeningFcn(hObject, eventdata, handles, varargin)
      % This function has no output args, see OutputFcn.
      % hObject handle to figure
      % eventdata reserved - to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)
      % varargin command line arguments to ipwebcamGUI (see VARARGIN)

      % Choose default command line output for ipwebcamGUI
      handles.output = hObject;

      % Update handles structure
      guidata(hObject, handles);

      % UIWAIT makes ipwebcamGUI wait for user response (see UIRESUME)
      % uiwait(handles.figure1);


      % --- Outputs from this function are returned to the command line.
      function varargout = ipwebcamGUI_OutputFcn(hObject, eventdata, handles)
      % varargout cell array for returning output args (see VARARGOUT);
      % hObject handle to figure
      % eventdata reserved - to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)

      % Get default command line output from handles structure
      varargout1 = handles.output;


      % --- Executes on button press in pushbutton1.
      function pushbutton1_Callback(hObject, eventdata, handles)
      % hObject handle to pushbutton1 (see GCBO)
      % eventdata reserved - to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)


      axes(handles.axes1);
      im=imread('c1.jpg');
      k=rgb2gray(im);

      % Threshold image - global threshold
      BW = imbinarize(k);

      % Invert mask
      BW = imcomplement(BW);

      %clear border
      BW = imclearborder(BW);

      % Create masked image.
      maskedImage = k;
      maskedImage(~BW) = 0;

      %count the white pixel in image
      whitepix = sum(BW(:));

      axis off;
      imshow(BW);
      clear


      % --- Executes on button press in pushbutton3.
      function pushbutton3_Callback(hObject, eventdata, handles)
      % hObject handle to pushbutton3 (see GCBO)
      % eventdata reserved - to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)

      T = trainClassifier2(trainingData);
      yfit = trainedClassifier.predictFcn(T);
      fprintf('result %d n',yfit );
      % set(handles.text2,'String',T);
      % drawnow();


      this file named ipwebcamGUI.m
      and the second trainClassifier2.m



      function [trainedClassifier, validationAccuracy] = trainClassifier2(trainingData)
      % trainClassifier(trainingData)
      % returns a trained classifier and its accuracy.
      % This code recreates the classification model trained in
      % Classification Learner app.
      %
      % Input:
      % trainingData: the training data of same data type as imported
      % in the app (table or matrix).
      %
      % Output:
      % trainedClassifier: a struct containing the trained classifier.
      % The struct contains various fields with information about the
      % trained classifier.
      %
      % trainedClassifier.predictFcn: a function to make predictions
      % on new data. It takes an input of the same form as this training
      % code (table or matrix) and returns predictions for the response.
      % If you supply a matrix, include only the predictors columns (or
      % rows).
      %
      % validationAccuracy: a double containing the accuracy in
      % percent. In the app, the History list displays this
      % overall accuracy score for each model.
      %
      % Use the code to train the model with new data.
      % To retrain your classifier, call the function from the command line
      % with your original data or new data as the input argument trainingData.
      %
      % For example, to retrain a classifier trained with the original data set
      % T, enter:
      % [trainedClassifier, validationAccuracy] = trainClassifier(T)
      %
      % To make predictions with the returned 'trainedClassifier' on new data T,
      % use
      % yfit = trainedClassifier.predictFcn(T)
      %
      % To automate training the same classifier with new data, or to learn how
      % to programmatically train classifiers, examine the generated code.

      % Auto-generated by MATLAB on 03-Mar-2019 14:35:31


      % Extract predictors and response
      % This code processes the data into the right shape for training the
      % classifier.
      inputTable = trainingData;
      predictorNames = 'VarName1';
      predictors = inputTable(:, predictorNames);
      response = inputTable.A;
      isCategoricalPredictor = [false];

      % Train a classifier
      % This code specifies all the classifier options and trains the classifier.
      classificationTree = fitctree(...
      predictors, ...
      response, ...
      'SplitCriterion', 'gdi', ...
      'MaxNumSplits', 100, ...
      'Surrogate', 'off', ...
      'ClassNames', 'A'; 'B'; 'C');

      % Create the result struct with predict function
      predictorExtractionFcn = @(t) t(:, predictorNames);
      treePredictFcn = @(x) predict(classificationTree, x);
      trainedClassifier.predictFcn = @(x) treePredictFcn(predictorExtractionFcn(x));

      % Add additional fields to the result struct
      trainedClassifier.RequiredVariables = 'VarName1';
      trainedClassifier.ClassificationTree = classificationTree;
      trainedClassifier.About = 'This struct is a trained classifier exported from Classification Learner R2016a.';
      trainedClassifier.HowToPredict = sprintf('To make predictions on a new table, T, use: n yfit = c.predictFcn(T) nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedClassifier''. n nThe table, T, must contain the variables returned by: n c.RequiredVariables nVariable formats (e.g. matrix/vector, datatype) must match the original training data. nAdditional variables are ignored. n nFor more information, see <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appclassification_exportmodeltoworkspace'')">How to predict using an exported model</a>.');

      % Extract predictors and response
      % This code processes the data into the right shape for training the
      % classifier.
      inputTable = trainingData;
      predictorNames = 'VarName1';
      predictors = inputTable(:, predictorNames);
      response = inputTable.A;
      isCategoricalPredictor = [false];

      % Perform cross-validation
      partitionedModel = crossval(trainedClassifier.ClassificationTree, 'KFold', 5);

      % Compute validation accuracy
      validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');

      % Compute validation predictions and scores
      [validationPredictions, validationScores] = kfoldPredict(partitionedModel);


      the program counts the white pixel in the image.And the "whitepix" variable would be the expected input..the output will be A,B,C..



      this part is where i am having a trouble..i want to display the output when button is clicked.im new to this please help me.



      function pushbutton3_Callback(hObject, eventdata, handles)
      % hObject handle to pushbutton3 (see GCBO)
      % eventdata reserved - to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)

      T = trainClassifier2(trainingData);
      yfit = trainedClassifier.predictFcn(T);
      fprintf('result %d n',yfit );









      share|improve this question














      i having trouble to call the function from the generated code and i want to display the output in command window.



      function varargout = ipwebcamGUI(varargin)
      % IPWEBCAMGUI MATLAB code for ipwebcamGUI.fig
      % IPWEBCAMGUI, by itself, creates a new IPWEBCAMGUI or raises the existing
      % singleton*.
      %
      % H = IPWEBCAMGUI returns the handle to a new IPWEBCAMGUI or the handle to
      % the existing singleton*.
      %
      % IPWEBCAMGUI('CALLBACK',hObject,eventData,handles,...) calls the local
      % function named CALLBACK in IPWEBCAMGUI.M with the given input arguments.
      %
      % IPWEBCAMGUI('Property','Value',...) creates a new IPWEBCAMGUI or raises the
      % existing singleton*. Starting from the left, property value pairs are
      % applied to the GUI before ipwebcamGUI_OpeningFcn gets called. An
      % unrecognized property name or invalid value makes property application
      % stop. All inputs are passed to ipwebcamGUI_OpeningFcn via varargin.
      %
      % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
      % instance to run (singleton)".
      %
      % See also: GUIDE, GUIDATA, GUIHANDLES

      % Edit the above text to modify the response to help ipwebcamGUI

      % Last Modified by GUIDE v2.5 07-Mar-2019 22:03:50

      % Begin initialization code - DO NOT EDIT
      gui_Singleton = 1;
      gui_State = struct('gui_Name', mfilename, ...
      'gui_Singleton', gui_Singleton, ...
      'gui_OpeningFcn', @ipwebcamGUI_OpeningFcn, ...
      'gui_OutputFcn', @ipwebcamGUI_OutputFcn, ...
      'gui_LayoutFcn', [] , ...
      'gui_Callback', []);
      if nargin && ischar(varargin1)
      gui_State.gui_Callback = str2func(varargin1);
      end

      if nargout
      [varargout1:nargout] = gui_mainfcn(gui_State, varargin:);
      else
      gui_mainfcn(gui_State, varargin:);
      end
      % End initialization code - DO NOT EDIT


      % --- Executes just before ipwebcamGUI is made visible.
      function ipwebcamGUI_OpeningFcn(hObject, eventdata, handles, varargin)
      % This function has no output args, see OutputFcn.
      % hObject handle to figure
      % eventdata reserved - to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)
      % varargin command line arguments to ipwebcamGUI (see VARARGIN)

      % Choose default command line output for ipwebcamGUI
      handles.output = hObject;

      % Update handles structure
      guidata(hObject, handles);

      % UIWAIT makes ipwebcamGUI wait for user response (see UIRESUME)
      % uiwait(handles.figure1);


      % --- Outputs from this function are returned to the command line.
      function varargout = ipwebcamGUI_OutputFcn(hObject, eventdata, handles)
      % varargout cell array for returning output args (see VARARGOUT);
      % hObject handle to figure
      % eventdata reserved - to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)

      % Get default command line output from handles structure
      varargout1 = handles.output;


      % --- Executes on button press in pushbutton1.
      function pushbutton1_Callback(hObject, eventdata, handles)
      % hObject handle to pushbutton1 (see GCBO)
      % eventdata reserved - to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)


      axes(handles.axes1);
      im=imread('c1.jpg');
      k=rgb2gray(im);

      % Threshold image - global threshold
      BW = imbinarize(k);

      % Invert mask
      BW = imcomplement(BW);

      %clear border
      BW = imclearborder(BW);

      % Create masked image.
      maskedImage = k;
      maskedImage(~BW) = 0;

      %count the white pixel in image
      whitepix = sum(BW(:));

      axis off;
      imshow(BW);
      clear


      % --- Executes on button press in pushbutton3.
      function pushbutton3_Callback(hObject, eventdata, handles)
      % hObject handle to pushbutton3 (see GCBO)
      % eventdata reserved - to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)

      T = trainClassifier2(trainingData);
      yfit = trainedClassifier.predictFcn(T);
      fprintf('result %d n',yfit );
      % set(handles.text2,'String',T);
      % drawnow();


      this file named ipwebcamGUI.m
      and the second trainClassifier2.m



      function [trainedClassifier, validationAccuracy] = trainClassifier2(trainingData)
      % trainClassifier(trainingData)
      % returns a trained classifier and its accuracy.
      % This code recreates the classification model trained in
      % Classification Learner app.
      %
      % Input:
      % trainingData: the training data of same data type as imported
      % in the app (table or matrix).
      %
      % Output:
      % trainedClassifier: a struct containing the trained classifier.
      % The struct contains various fields with information about the
      % trained classifier.
      %
      % trainedClassifier.predictFcn: a function to make predictions
      % on new data. It takes an input of the same form as this training
      % code (table or matrix) and returns predictions for the response.
      % If you supply a matrix, include only the predictors columns (or
      % rows).
      %
      % validationAccuracy: a double containing the accuracy in
      % percent. In the app, the History list displays this
      % overall accuracy score for each model.
      %
      % Use the code to train the model with new data.
      % To retrain your classifier, call the function from the command line
      % with your original data or new data as the input argument trainingData.
      %
      % For example, to retrain a classifier trained with the original data set
      % T, enter:
      % [trainedClassifier, validationAccuracy] = trainClassifier(T)
      %
      % To make predictions with the returned 'trainedClassifier' on new data T,
      % use
      % yfit = trainedClassifier.predictFcn(T)
      %
      % To automate training the same classifier with new data, or to learn how
      % to programmatically train classifiers, examine the generated code.

      % Auto-generated by MATLAB on 03-Mar-2019 14:35:31


      % Extract predictors and response
      % This code processes the data into the right shape for training the
      % classifier.
      inputTable = trainingData;
      predictorNames = 'VarName1';
      predictors = inputTable(:, predictorNames);
      response = inputTable.A;
      isCategoricalPredictor = [false];

      % Train a classifier
      % This code specifies all the classifier options and trains the classifier.
      classificationTree = fitctree(...
      predictors, ...
      response, ...
      'SplitCriterion', 'gdi', ...
      'MaxNumSplits', 100, ...
      'Surrogate', 'off', ...
      'ClassNames', 'A'; 'B'; 'C');

      % Create the result struct with predict function
      predictorExtractionFcn = @(t) t(:, predictorNames);
      treePredictFcn = @(x) predict(classificationTree, x);
      trainedClassifier.predictFcn = @(x) treePredictFcn(predictorExtractionFcn(x));

      % Add additional fields to the result struct
      trainedClassifier.RequiredVariables = 'VarName1';
      trainedClassifier.ClassificationTree = classificationTree;
      trainedClassifier.About = 'This struct is a trained classifier exported from Classification Learner R2016a.';
      trainedClassifier.HowToPredict = sprintf('To make predictions on a new table, T, use: n yfit = c.predictFcn(T) nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedClassifier''. n nThe table, T, must contain the variables returned by: n c.RequiredVariables nVariable formats (e.g. matrix/vector, datatype) must match the original training data. nAdditional variables are ignored. n nFor more information, see <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appclassification_exportmodeltoworkspace'')">How to predict using an exported model</a>.');

      % Extract predictors and response
      % This code processes the data into the right shape for training the
      % classifier.
      inputTable = trainingData;
      predictorNames = 'VarName1';
      predictors = inputTable(:, predictorNames);
      response = inputTable.A;
      isCategoricalPredictor = [false];

      % Perform cross-validation
      partitionedModel = crossval(trainedClassifier.ClassificationTree, 'KFold', 5);

      % Compute validation accuracy
      validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');

      % Compute validation predictions and scores
      [validationPredictions, validationScores] = kfoldPredict(partitionedModel);


      the program counts the white pixel in the image.And the "whitepix" variable would be the expected input..the output will be A,B,C..



      this part is where i am having a trouble..i want to display the output when button is clicked.im new to this please help me.



      function pushbutton3_Callback(hObject, eventdata, handles)
      % hObject handle to pushbutton3 (see GCBO)
      % eventdata reserved - to be defined in a future version of MATLAB
      % handles structure with handles and user data (see GUIDATA)

      T = trainClassifier2(trainingData);
      yfit = trainedClassifier.predictFcn(T);
      fprintf('result %d n',yfit );






      matlab image-processing machine-learning






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 14:15









      eihmz abeeihmz abe

      12




      12






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065036%2fhow-to-use-the-generated-code-from-classification-learner-app-in-matlab-to-predi%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065036%2fhow-to-use-the-generated-code-from-classification-learner-app-in-matlab-to-predi%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229