MATLAB Answers

Why do I get an "Invalid value returned."-error when assigning a string property inside a loop using v18b?

When initializing an object of class "Matlab_Dbl_Str_Test" (class in attached file below) in Matlab v18b the result is the following:
When avoiding the try catch block one gets the above mentioned error.
It appears that properties of type string cannot be assigned in a loop whereas properties of type double are perfectly fine.
Is this a known bug or wanted feauture?
%% --------------------------------------------------------------------------
classdef Matlab_Dbl_Str_Test < handle
%UNTITLED2 Summary of this class goes here
% Detailed explanation goes here
properties( Constant )
TEST_STR = ["a";"b";"c"];
TEST_DBL = [0;1;2];
end
properties( SetAccess = protected )
test_str@string as vector;
test_dbl@double as vector;
end
methods
function obj = Matlab_Dbl_Str_Test()
for i = 1:3
try obj.test_str(i) = Matlab_Dbl_Str_Test.TEST_STR(i,1);end
try obj.test_dbl(i) = Matlab_Dbl_Str_Test.TEST_DBL(i,1);end
end
end
function test( obj )
for i = 1:3
try obj.test_str(i) = Matlab_Dbl_Str_Test.TEST_STR(i,1);end
try obj.test_dbl(i) = Matlab_Dbl_Str_Test.TEST_DBL(i,1);end
end
end
end
end

  댓글 수: 0

로그인 to comment.

답변 수: 1

per isakson 님의 답변 16 Jul 2019
per isakson 님이 편집함. 16 Jul 2019
 채택된 답변

The syntax of your properties definitions
properties( SetAccess = protected )
test_str@string as vector;
test_dbl@double as vector;
end
looks strange to me and I failed to find it in the documentation. (Maybe I didn't try hard enough.)
This modified version of your class works with R2018b
classdef Matlab_Dbl_Str_Test < handle
properties( Constant )
TEST_STR = ["a";"b";"c"];
TEST_DBL = [0;1;2];
end
properties( SetAccess = protected )
test_str (:,1) string
test_dbl (:,1) double
end
methods
function obj = Matlab_Dbl_Str_Test()
for i = 1:3
try
obj.test_str(i) = Matlab_Dbl_Str_Test.TEST_STR(i,1);
catch me
getReport( me )
end
try
obj.test_dbl(i) = Matlab_Dbl_Str_Test.TEST_DBL(i,1);
catch me
getReport( me )
end
end
end
function test( obj )
for i = 1:3
try
obj.test_str(i) = Matlab_Dbl_Str_Test.TEST_STR(i,1);
catch me
getReport( me )
end
try
obj.test_dbl(i) = Matlab_Dbl_Str_Test.TEST_DBL(i,1);
catch me
getReport( me )
end
end
end
end
end
Test
>> mbs = Matlab_Dbl_Str_Test
mbs =
Matlab_Dbl_Str_Test with properties:
TEST_STR: [3×1 string]
TEST_DBL: [3×1 double]
test_str: [3×1 string]
test_dbl: [3×1 double]
>> mbs.test_str
ans =
3×1 string array
"a"
"b"
"c"

  댓글 수: 0

로그인 to comment.



Translated by