この質問は閉じられています。 編集または回答するには再度開いてください。

Variable updates for a brief moment before returning to the original value.

1 回表示 (過去 30 日間)
David Eissner
David Eissner 2019 年 10 月 8 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
classdef AircraftClass < ModelClass
properties(SetAccess = private)
P_req % Power Required
P_avail % Power Available
function obj = AircraftClass(B, C, D, E, R, P, T, Z)
% Class constructor
obj.B = B;
obj.C = C;
obj.D = D;
obj.E = E;
obj.R = R;
obj.P = P;
obj.T = T;
obj.Z = Z;
obj.B = B.A;
function [P_avail, P_req, FuelF_req] = calc_point(self, xmsnLimitFlag)
if nargin==1; xmsnLimitFlag=1; end
P_avail = self.get_PowerAvailable(xmsnLimitFlag);
self.P_avail = P_avail;
[P_req, FuelF_req] = self.get_FuelFlow_PowerRequired();
I'm having issues with the "self.P_avail = P_avail" line.
Basically in short, P_avail = self.get_PowerAvailable(xmsnLimitiFlag) updates a list of power data.
However, when I go to assign this new data to self.P_avail, the data updates for a second or two and then reverts back to the old data.
Why is this occuring and how can I make the update permanent?
  3 件のコメント
Adam 2019 年 10 月 10 日
At what point in the code are you looking at the value of self.P_avail? How do you know it updates for a second or two?
David Eissner
David Eissner 2019 年 10 月 16 日
Because I can place a breakpoint at the line "self.P_avail = P_avail;"
At this point, I can see that P_avail has the correct values. When I execute the line "self.P_avail = P_avail;", I can see my object in the Variable window update and then revert back.

回答 (3 件)

Steven Lord
Steven Lord 2019 年 10 月 8 日
Does the get_PowerAvailable method of this class return a number, an instance of this class, or something else?
When you say "reverts back to the old data" are you looking at the self object inside the calc_point method or are you looking at the object in the workspace that you passed into the calc_point method? If the latter, I'm guessing ModelClass is not a handle class and you don't return the object from the method, so the changes are made only to the copy of the object in the method workspace.
If you want the modified object to be available outside the method, you need to either return it from the method or make your class a handle class. See this documentation page for a discussion of the difference between handle and value classes.
  2 件のコメント
David Eissner
David Eissner 2019 年 10 月 8 日
It returns a list of values.
As to your second question, the second. The self object I'm referring to is the one within the Aircraft Class.
David Eissner
David Eissner 2019 年 10 月 8 日
ModelClass is a handle class as defined below.
classdef ModelClass < handle

Martin C.
Martin C. 2019 年 10 月 8 日
hmm you might want to use '< handle' if you want to make changes to an object within a method.
Not sure this is your issue.
Could you provide your whole class to better understand what might be going on?
  1 件のコメント
David Eissner
David Eissner 2019 年 10 月 8 日
I'm confused as this class is already using a handle.

David Eissner
David Eissner 2019 年 10 月 10 日
Anyone have any other thoughts?
I am at a loss why Matlab would change a variable that I have assinged specifically and then revert back to the old values without having stepped forward any farther in the code. That seems impossible in a sense.

Community Treasure Hunt

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

Start Hunting!

Translated by