Using a generated struct within a function

2 views (last 30 days)
Jamie England
Jamie England on 20 Mar 2019
Edited: Stephen23 on 4 Jun 2019
Hi all, I have generated some functions using the classification learner app and exported them to the work space. They appear within the workspace as a 1x1 struct. I have attempted to create a funtion that will read newly generated data and input into these classification functions to output what class the data is approximatley in. The issue is when I attempt to use the Train function there is an error as the x.predictFcn( C ) is not found. Is there a way I can put the predict function within my function? Thank you
function [C2,C10,C100] = Train(Pump,Date)
c = sprintf('%s_Current.%s.csv',Pump,Date)
if exist(c)
C = xlsread(c);
C = C([1:end],[2:end]);
C2 = mean(C2A635.predictFcn(C));
C10 = mean(C10A461.predictFcn(C));
C100 = mean(C100A208.predictFcn(C));
else
C2 = 'NaN';
C10 = 'NaN';
C100 = 'NaN';
end
end

Answers (1)

Simon Mählkvist
Simon Mählkvist on 4 Jun 2019
You can't access your local workspace from the function. Try adding x.predictFcn() to the function input as such:
function [C2,C10,C100] = Train(Pump,Date,x.predictFcn)
  3 Comments
Simon Mählkvist
Simon Mählkvist on 4 Jun 2019
I was trying to be generall. The function Train need all data it is going to use and as such the functions you have created needs to be insterted or saved as proper functions.
Try this instead:
function [C2,C10,C100] = Train(Pump,Date,C2A635.predictFcn,C10A461.predictFcn,C100A208.predictFcn)
c = sprintf('%s_Current.%s.csv',Pump,Date)
if exist(c)
C = xlsread(c);
C = C([1:end],[2:end]);
C2 = mean(C2A635.predictFcn(C));
C10 = mean(C10A461.predictFcn(C));
C100 = mean(C100A208.predictFcn(C));
else
C2 = 'NaN';
C10 = 'NaN';
C100 = 'NaN';
end
end
Stephen23
Stephen23 on 4 Jun 2019
Edited: Stephen23 on 4 Jun 2019
function [C2,C10,C100] = Train(Pump,Date,C2A635.predictFcn,C10A461.predictFcn,C100A208.predictFcn)
% Not valid for a function definition: ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!