Error: Function definition not supported in this context. Create functions in code file.

4 visualizaciones (últimos 30 días)
I have a problem with launching a function I created. Can anybody help me what's wrong with this line?
error: Error: File: MetData3.m Line: 10 Column: 5
Function definition not supported in this context. Create functions in code file.
Code:
classdef MetData3
properties
WindSpeed
end
properties (Access=protected)
Gamma = 1.7934;
Beta = 4.1007;
end
function WeibData = WeibFormWind(theMetData)
try
WeibData=zeros(1,max(theMetData.WindSpeed));
for j=1:1:max(size(WeibData))
i=1;
for k=1:1:max(size(theMetData.WindSpeed))
if theMetData.WindSpeed(k)==j
WeibData(j)=WeibData(j)+theMetData.WindSpeed(k);
i=i+1;
end
end
end
WeibData=WeibData/sum(theMetData.WindSpeed);
bar(WeibData);
catch
disp('Problem')
end
end
function monthYield = monthWeibull(theMetData)
close all
montrYield = zeros(1,max(size(theMetData.monthAvrWS)));
for k=1:1:max(size(theMetData.monthAvrWS))
B = theMetData.Beta * theMetData.monthAvrWS(k);
v = 0:0.1:20;
Weib1 = @(u) (theMetData.Gamma/B).*(u*(1/B)).*exp(-(u*(1/B)).^2);
Weib2 = @(u) ((theMetData.Gamma/B).*(u*(1/B)).*exp(-(u*(1/B)).^2).sin((pi/2)*((u-theMetData.Vd)/(theMetData.Vo-theMetData.Vd))));
t1=integral(Weib1, theMetData.Vo, theMetData.Vg);
E1=(theMetData.Gamma/B)*theMetData.Nn*t1;
t2=integral(Weib2, theMetData.Vd, theMetData.Vo);
E2=(theMetData.Gamma/B)*theMetData.Nn*t2;
monthYield(k)=E1+E2;
curve=Weib1(v);
plot(v,curve);grid on,hold on;
xlabel('prędkość wiatru');ylabel('t/T');
end
hold off;
end
end

Respuesta aceptada

Steven Lord
Steven Lord el 25 de Mzo. de 2021
In a classdef file, functions that you want to be methods of the class need to be in a methods block. Note that in the example in the first section that the ordinaryMethod function is not directly inside the classdef block but is inside a methods block inside the classdef block.
Functions that you want to be local to the class (class-related functions) can be after the end matching the classdef keyword, but they cannot be called from outside the class even if you try calling them on an instance of the class. See the myUtilityFcn function later on that documentation page for an example.
  2 comentarios
Hubert Skiba
Hubert Skiba el 25 de Mzo. de 2021
Thank You!
I have one more question. It looks like the function works now but there is another problem with my main script.
Error massage:
Error using MetData3
Too many input arguments.
Error in test (line 15)
MyData(k)= MetData3(loc,Ws);
Code:
clear; clc; format short g;
dane = dir('Dane'); dane = dane(3:end);
for k = 1:1:max(size(dane))
a=horzcat('Dane/',dane(k).name);
[id1,kom]=fopen(a);
if(id1<0)
disp(kom)
end
[A]=fscanf(id1,'%f %f',[46 8760]);
fclose(id1);
A=A';
loc=dane(k).name;
Ws= A(:, 8);
MyData(k)= MetData3(loc,Ws);
end
clear A;
Steven Lord
Steven Lord el 25 de Mzo. de 2021
The code you posted originally does not include a constructor. Therefore you get the Miranda constructor:
"MATLAB® classes that do not explicitly define any class constructors have a default constructor method. This method returns an object of the class that is created with no input arguments. A class can define a constructor method that overrides the default constructor. An explicitly defined constructor can accept input arguments, initialize property values, call other methods, and perform other operations necessary to create objects of the class."
If you want to be able to specify data to be used in constructing the object, you should add a constructor to your class. As the guidelines on that documentation page state, though, your constructor should also be able to handle creating an instance of the object with no input arguments (using default or "reasonable" values for the parameters.)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Function Handles en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by