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

조회 수: 3 (최근 30일)
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

채택된 답변

per isakson
per isakson 2019년 7월 16일
편집: per isakson 2019년 7월 16일
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개)

Community Treasure Hunt

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

Start Hunting!

Translated by