MATLAB OOP: Return value from constructor not possible
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Milad Omidi
el 2 de Nov. de 2018
Comentada: Matt J
el 2 de Nov. de 2018
I have a simple class called P that is supposed to Read and write a data structure to a file. It can be constructed like this:
P(filename,Data) % Construct object and write data to the file.
Now for the reading part I want to be able to get the Data structure by doing something like this:
Data = P(filename);
However MATLAB constructor is not allowed to return anything other than the object. So I am forced to change the interface such that the object is first created and then some method is used to return the data structure like this:
P1 = P();
Data = P1.load(filename);
Is there a better way to design the class (or any other solution) that reads the data with a single command just like writing.
Thanks, Milad
0 comentarios
Respuesta aceptada
Matt J
el 2 de Nov. de 2018
Editada: Matt J
el 2 de Nov. de 2018
To avoid creating an object, you can use a static class method.
methods (Static)
function Data=read(filename)
...
end
end
and then
>> Data=P.read(filename);
Or, you could just create a non-class function,
Data=readP(filename);
0 comentarios
Más respuestas (2)
Caglar
el 2 de Nov. de 2018
Editada: Caglar
el 2 de Nov. de 2018
You can use a static method. They can be used like functions. You could do Data=P.load(filename) with them but they do not automatically create an object.
On the other hand, MATLAB constructor is allowed to return variables other than the object.
classdef P
properties
end
methods
function [obj,test] = P(inputArg1)
test=inputArg1*2;
end
end
end
>>[P1,result]=P(3)
P1 =
P with no properties.
result =
6
0 comentarios
Guillaume
el 2 de Nov. de 2018
However MATLAB constructor is not allowed to return anything other than the object.
I have not encountered any programming language where the object constructor returns anything other than the object. That's the definition of the constructor: special function that creates and return the object.
The whole design of your class doesn't make sense. This would make a lot more sense:
obj = P(filename); %create reader/writer that access a specific file
obj.write(data); %write data to file assigned to object
data = obj.read; %read data from file assigned to object
Otherwise, as you explained it, I see no need for a class. Regular functions would do the job.
3 comentarios
Guillaume
el 2 de Nov. de 2018
Yes, well don't do that! I'm not sure if that wouldn't break under some circumstances and that's certainly not good designed and may not even be supported in future versions of matlab.
Matlab's own documentation is clear that a constructor should have only one output argument: "The only output argument from a constructor is the object constructed."
Matt J
el 2 de Nov. de 2018
I think one can make a case for additional output arguments from constructors. It can be useful in debugging for example, if you want to return intermediate calculations to the workspace for further examination.
Ver también
Categorías
Más información sobre Software Development Tools en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!