Property assignment is not allowed when the object is empty. Use subscripted assignment to create an array element.

조회 수: 78 (최근 30일)
Hello!
I was developed a software with app designer and connect all values in a constructor class (this connection is running). But when I click in Run button, I need to access the function Positive_Time(load, tnt, analysis) in Class_Load and presents this error. Can you help me?
Property assignment is not allowed when the object is empty. Use subscripted
assignment to create an array element.
Error in Class_Load/Positive_Time (line 222)
load.tnt = tnt;
Here my Class_TNT
classdef Class_TNT
properties (SetAccess = public, GetAccess = public)
Z = 0;
W = 0;
type = 0;
negative = 0;
end
methods
function tnt = Class_TNT(W, Z, type, negative)
if (nargin > 0)
tnt.Z = Z;
tnt.W = W;
tnt.type = type;
tnt.negative = negative;
else
tnt.Z = 5;
tnt.W = 10;
tnt.type = 1;
tnt.negative = 1;
end
end
end
end
Here, my Class_Load
classdef Class_Load
properties (SetAccess = public, GetAccess = public)
tnt Class_TNT % TNT's properties
analysis Class_TypeAnalysis % Type Analysis propertie
td = 0; % Variable about the time that is during the
end
methods
function load = Class_Load(tnt, analysis)
if (nargin > 0)
load.Positive_Time(load, tnt, analysis);
else
load.tnt = Class_TNT(); % Empty constructor
end
end
end
methods
function load = Positive_Time(load, tnt, analysis)
load.tnt = tnt;
load.analysis = analysis;
type = load.tnt.type;
Z = load.tnt.Z;
W = load.tnt.W;
switch type
case 1
if (0.20993 < Z && Z <= 1.0355)
load.td = 0.001 * ( 2.2064 * Z^3 - 0.3363 * Z^2 - ...
0.5644 * Z + 0.3756 ) * ( W^(1/3) );
elseif (4.001 < Z)
load.td = 0.001 * ( 1.5602 * log(Z) +1.2416 ) * ...
( W^(1/3) );
end
case 2
if (0.2210 < Z && Z <= 0.7790)
load.td = 0.001 * ( 4.2668 * Z^3 - 1.9736 * Z^2 + ...
0.132 * Z + 0.2145 ) * ( W^(1/3) );
elseif (3.7612 < Z)
load.td = 0.001 * ( - 0.0029 * Z^2 + 0.2159 * Z + ...
2.1382 ) * ( W^(1/3) );
end
end
end
end
end
  댓글 수: 6
Image Analyst
Image Analyst 2020년 6월 23일
Of course, everyone uses breakpoints.
load() is a built in function. It's a very, very bad idea to call your returned structure "load" or any other built-in function name. Pick another name than load.
Attach your class file, and your test file that calls it if you want more help.
Walter Roberson
Walter Roberson 2020년 6월 23일
function load = Class_Load(tnt, analysis)
if (nargin > 0)
load.Positive_Time(load, tnt, analysis);
You have no class property named load so the load in the argument does not refer to any class property or any input variable. You have an output variable named load but you have not assigned to it, so it does not exist yet. Therefore the load refers to the load() function, which would default to loading matlab.mat if it can find it, returning the loaded variables in the form of a struct. But then load.Positive_Time does not exist to pass the result into.

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2020년 6월 23일
편집: per isakson 2020년 6월 23일
I fail to reproduce the error you report. However, that should not surprise since your description of how you execute the classes is vague.
My first test
tnt = Class_TNT( 17, 7, 1, 1 );
ana = Class_TypeAnalysis;
ladda = Class_Ladda( tnt, ana )
outputs
ladda =
Class_Ladda with properties:
tnt: [0×0 Class_TNT]
analysis: [0×0 Class_TypeAnalysis]
td: 0
>>
No error, an instance of Class_Ladda is created, but the values of the properties tnt and analysis are empty.
My second test
I replaced the statements
this.Positive_Time( tnt, analysis );
in the constructor of Class_Ladda by
this = this.Positive_Time( tnt, analysis );
since it's a value class. Now the output looks ok
ladda =
Class_Ladda with properties:
tnt: [1×1 Class_TNT]
analysis: [1×1 Class_TypeAnalysis]
td: 0.0110
>>
The classes are
classdef Class_TNT
properties (SetAccess = public, GetAccess = public)
Z = 0;
W = 0;
type = 0;
negative = 0;
end
methods
function tnt = Class_TNT( W, Z, type, negative )
if (nargin > 0)
tnt.Z = Z;
tnt.W = W;
tnt.type = type;
tnt.negative = negative;
else
tnt.Z = 5;
tnt.W = 10;
tnt.type = 1;
tnt.negative = 1;
end
end
end
end
classdef Class_Ladda
properties ( SetAccess = public, GetAccess = public )
tnt Class_TNT % TNT's properties
analysis Class_TypeAnalysis % Type Analysis propertie
td = 0; % Variable about the time that is during the
end
methods
function this = Class_Ladda( tnt, analysis )
if (nargin > 0)
this.Positive_Time( tnt, analysis );
else
this.tnt = Class_TNT(); % Empty constructor
end
end
end
methods
function this = Positive_Time( this, tnt, analysis )
this.tnt = tnt;
this.analysis = analysis;
type = this.tnt.type;
Z = this.tnt.Z;
W = this.tnt.W;
switch type
case 1
if (0.20993 < Z && Z <= 1.0355)
this.td = 0.001 * ( 2.2064 * Z^3 - 0.3363 * Z^2 - ...
0.5644 * Z + 0.3756 ) * ( W^(1/3) );
elseif (4.001 < Z)
this.td = 0.001 * ( 1.5602 * log(Z) +1.2416 ) * ...
( W^(1/3) );
end
case 2
if (0.2210 < Z && Z <= 0.7790)
this.td = 0.001 * ( 4.2668 * Z^3 - 1.9736 * Z^2 + ...
0.132 * Z + 0.2145 ) * ( W^(1/3) );
elseif (3.7612 < Z)
this.td = 0.001 * ( - 0.0029 * Z^2 + 0.2159 * Z + ...
2.1382 ) * ( W^(1/3) );
end
otherwise
error( 'Positive_Time:Unknown', 'Invalid type: %d', type )
end
end
end
end
I've replaced "load" by "ladda"
  댓글 수: 3
per isakson
per isakson 2020년 6월 24일
편집: per isakson 2020년 6월 24일
Thank you for confiming that my answer worked for you!
  • TL;NR
  • hard to fullfill all the recommendation
  • however, it's worth reading
There are many types of questions. Regarding "I need help, my code throws an error":
  • To spot errors by code inspections is difficult (except for trivial syntax errors) and tedious
  • Provide code and an instruction, which makes it easy to reproduce the error. Code, in excess of a screenful, should be attached with help of the paper-clip icon. Don't forget appropriate values of input arguments.
  • Template: 1) A very short background. 2) On my R2018b/Win10 this little script produces the following error. 3) The files, File1.m and File2.m are attached. 4) Some comments which shows your ideas on the error and that you made an effort.
  • Always include the full error message (as you did).
  • A Minimal working example is always good, but is often easier said than done. Making a MWE is often a way to solve the problem and get deeper insight.
  • Never ever just reply "Doesn't work:(" to an answer. (You didn't.)
Finally, I opened your Class_Load in the editor and clicked the green triangle and got
>> Class_Load
ans =
Class_Load with properties:
tnt: [1×1 Class_TNT]
analysis: [0×0 Class_TypeAnalysis]
td: 0
>>
in the command window.
Ana Reis
Ana Reis 2020년 6월 24일
yes yes! I'm going to read this. Thank you for the help and for this TUTORIAL to ask questions.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Web Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by