tailieunhanh - CRC.Press A Guide to MATLAB Object Oriented Programming May.2007 Episode 1 Part 8

Tham khảo tài liệu ' a guide to matlab object oriented programming episode 1 part 8', kỹ thuật - công nghệ, cơ khí - chế tạo máy phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 114 A Guide to MATLAB Object-Oriented Programming IMPROVING The improved implementation for subsref is included in Code Listing 48. The primary difference between this version and the version in occurs in the dot-reference case. Lines 5-9 are different and only those lines are described below. A detailed description of the other lines can be found in . Rather than including a case for each public variable name public values are accessed using get. By eliminating the public-name cases subsref no longer contains classspecific code. Code Listing 48 Improved Implementation for 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 function varargout subsref this index switch index 1 .type case . if isempty this varargout cell 0 else varargout cell 1 max length this nargout end try varargout get this index catch rethrow lasterror end if length index 1 if length this 1 varargout subsref varargout index 2 end else err_id err_msg array_reference_error index 2 . type 21 22 23 24 25 26 27 28 29 30 31 32 error err_id err_msg end end case this_subset this index 1 .subs if length index 1 varargout this_subset else trick subsref into returning more than 1 ans varargout cell size this_subset varargout subsref this_subset index 2 end 33 34 35 end case Simplify Using get set fieldnames and struct 115 36 error class this object is not a cell array 37 38 otherwise 39 error Unexpected of index 1 .type 40 end 41 42 if length varargout 1 nargout 1 43 if iscellstr varargout any cellfun isempty varargout 44 varargout varargout 45 else 46 try 47 varargout varargout 48 catch 49 varargout varargout 50 end 51 end 52 end Lines 5-9 preallocate the output cell array. When get is called in line 11 the length of varargout will determine get s value of nargout. Preallocation is important because it tricks MATLAB into passing the proper nargout value. If the object is empty line 6 preallocates an empty varargout. Otherwise line 8 preallocates using the .

TỪ KHÓA LIÊN QUAN