cant get object's method to return multiple outputs

30 ビュー (過去 30 日間)
vanni meier
vanni meier 2025 年 1 月 28 日 17:46
コメント済み: Steven Lord 2025 年 1 月 30 日 14:50
Hi, i cant get my method to return multiple outputs.
I have my object folder with the classdef
classdef crm < handle
%...
methods (Access = public)
[Tout,Yout]=runTimeDependent(self,n_0,tdata,nedata,Tedata)
end
end
And my function definition in a separate file inside the @crm folder
function [Tout,Yout]=runTimeDependent(self,n_0,tdata,nedata,Tedata)
TSPAN = [tdata(1) tdata(end)];
[Tout,Yout]=ode15s(@odefun,TSPAN,n_0);
function dydt=odefun(t,y)
% ... ode function here
end
end
If i build my object and run the method everything works but if i ask 2 outputs i get an error
c=crm();
% definemy parameters ( Y0, t, ne, Te)
c.runTimeDependent(Y0,t,ne,Te); % This line works
[Tout, Yout] = c.runTimeDependent(Y0,t,ne,Te); %this line throws an error
Only when i ask both my outputs do i get an error
Error using indexing
Too many output arguments.
Error in test_timedep (line 4)
[Tout, Yout] = c.runTimeDependent(Y0,t,ne,Te);
  2 件のコメント
Walter Roberson
Walter Roberson 2025 年 1 月 28 日 18:42
編集済み: Walter Roberson 2025 年 1 月 28 日 18:44
It would be interesting to see the output of
[Tout, Yout] = runTimeDependent(c,Y0,t,ne,Te);
and of
[Tout] = c.runTimeDependent(Y0,t,ne,Te);
vanni meier
vanni meier 2025 年 1 月 29 日 7:57
編集済み: vanni meier 2025 年 1 月 29 日 8:17
EDIT:
The first case is working. is there a way to call this function in the traditional c.method() way and get it to work?
In the second case you get the vector of times Tout, at which the ode was solved.
If you are wondering, ode15s() sucessfully returns both Tout and Yout. Only runTimeDependent() fails to return multiple outputs.

サインインしてコメントする。

回答 (1 件)

Steven Lord
Steven Lord 2025 年 1 月 29 日 16:29
From the error message I suspect the class overloads indexing, either by overloading subsref or using one or more of the indexing mixins (specifically the matlab.mixin.indexing.RedefinesDot Class). Is that correct? If so, does the class overload either of the methods dotListLength or dotReference or does it inherit from either of the classes matlab.mixin.indexing.ForbidsPublicDotMethodCall, or matlab.mixin.indexing.OverridesPublicDotMethodCall method to handle the case where the "reference" is actually a call to a class method?
  2 件のコメント
vanni meier
vanni meier 2025 年 1 月 30 日 8:21
編集済み: vanni meier 2025 年 1 月 30 日 8:44
Correct, the class overloads subref. Althoug I'm not the autor of this portion of code so I guess i need to go read documentation on that. dotListLength and dotReference are not overloaded.
Just in case, here is the subrefs method
function result = subsref(self, s) % Overload indexing in order to use names as indices
% The struct s contains the location and inputs where a () was called
for i = 1:length(s)
% Treat different types of inputs separately: numbers, text or vectors like 3:7
if iscell(s(i).subs)
for j = 1:length(s(i).subs)
if ischar(s(i).subs{j})
labels(j) = string(s(i).subs{j}); %string(s(i).subs{:});
else
labels(j) = 0; % Do not modify non-char inputs
end
end
else
labels = string(s(i).subs);
end
% Find all instances when an index corresponds to a name
names = [self.name];
isName = zeros(1,length(labels));
for j = 1:length(labels)
if any(strcmpi(labels(j),names))
isName(j) = 1;
end
end
% Convert them to integer index
if (any(isName))
for j = find(isName) % % Loop over all name occurrences 1:length(labels)
s(i).subs(j) = {self.getname(labels(j))};
end
end
end
% The modified inputs are given to the default () handler
try
result = builtin('subsref', self, s);
catch
builtin('subsref', self, s);
end
end
Steven Lord
Steven Lord 2025 年 1 月 30 日 14:50
Set a breakpoint in that function then run this command:
[Tout] = c.runTimeDependent(Y0,t,ne,Te);
You should see that MATLAB will stop at the breakpoint in the class's subsref method.
Your subsref should probably be implemented with varargout as an output argument, and when subsref invokes one of the class's methods it should use the technique shown in the "Assigning to a Comma-Separated List" section on this documentation page. You may also need to overload the numArgumentsFromSubscript method; see the second example on that documentation page for an example of a class that both overloads that method and has a subsref overload returning varargout.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFunctions についてさらに検索

タグ

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by