MATLAB: Help with understanding a piece of code

classMATLABoop

Hi all!
Could you please help me understand this piece of code here?
classdef Tire<handle
properties (Constant)
DEFAULT_DIR = 'C:\Users\Vicente Silva\Desktop\';
end
properties
Name;
Dir;
end
properties
% here comes the declaration of a bunch of properties,
% which are not important for understanding the code itself
end
methods
function tire = Tire(varargin)
tire.Name = varargin{1};
if nargin > 1
tire.Dir = varargin{2};
else
tire.Dir = Tire.DEFAULT_DIR;
end
try
load([tire.Dir tire.Name '.mat']);
catch
tire = tire.LoadFromTir([tire.Name '.tir']);
tire.Fzo = tire.FNOMIN;
end
end
function tire = LoadFromTir(tire, tir_filename)
tirfile = fopen([tire.Dir tir_filename], 'r');
line = fgetl(tirfile);
while ischar(line)
if regexp(line, '^[A-Z]')
prop = strsplit(line,{'=' '$'});
propname = char(strtrim(prop{1}));
if isprop(tire, propname)
propval = char(strtrim(prop{2}));
if ~strncmpi(propval,'''',1)
tire.(propname) = str2double(propval);
else
tire.(propname) = strrep(propval, '''', '');
end
end
end
line = fgetl(tirfile);
end
fclose(tirfile);
end
function Save(tire)
save([tire.Dir tire.Name '.mat'], inputname(1));
end
I have used 'help' command for most of the commands I didn't know, but it didn't help understand much. More importantly, it surely didn't allow me to understand how to keep on writing pieces of code myself! Keep in mind, this is part of a work I'm currently developing with a friend of mine. But he is the computer sciences student, and I'm the mechanical engineering one. I only understand a bit of coding in MATLAB and Simulink. This here is practically russian for me. I have tried asking my friend for more help and clarifications, but it seems he is busy. I really wish I could do all this work on my own, but I'm at a much more superficial level of coding right now. My work is due at the 15th this month, and without any help I won't be able to finish everything. Please, help me!

Best Answer

Which part of it do you not understand? There are a number of different elements in that code and if you already understand some aspects it would be a waste of time someone explaining them in detail! So in relatively little detail:
  1. Properties blocks store class properties that are available to any function in the class, similar to variable in function scope, but referred to with the object scope when used (I assume you have no problem with these)
  2. Methods blocks contain the functions that you can call. In this case all may be called from external to the class, whether that is the intention or not.
  3. The first function is the constructor which takes a variable sized array of inputs and creates the actual object instance. It does no validation of the inputs, but the expected inputs can be worked out easily enough from the code which assigns them to the relevant properties. The body of the constructor appears to try to load data from a .mat file and if it fails it loads from a .tir file instead (which I assume is always valid since it has no error handling on that part).
  4. The second function is the loadFromTir called in the constructor when loading from .mat fails. The body of that uses regular expressions to extract the required information to store in the class properties using dynamic strings for the property names.
  5. The save function should theoertically save the tire name and directory to a mat file, but since inputname appears to be undefined I assume that function will crash.
The following syntax:
tire.(propname)
allows you to use dynamic strings to assign to properties, but it does come with the assumption that ' propname ' is something that evaluates to one of the properties defined in your properties blocks. It is a method I have used to good effect on a few occasions, but can easily become the source of difficult to track down bugs due to its obfuscation of the property names it is actually assigning to. Basically this will load in and assign all those properties you omitted as "not important for understanding the code itself".
The actual code in the load function is mostly just dynamic string manipulation with regular expressions and other string manipulation functions that trim off extra bits of the string to get the correct property names in the strings to then assign as mentioned above.
If you need more understanding of the exact nature of the regexp and string manipulations I will let someone else add that as it is something I battle with occasionally and forget quickly afterwards!! Your tags suggest it is the OOP aspect you are struggling with though.