Help with understanding a piece of code
Show older comments
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!
Accepted Answer
More Answers (3)
Don't computer science students learn to comment their code? The lack of comment should be reason enough to fail the assignment. At the very least inputs and outputs of functions should be documented with a short comment about the purpose of the function.
You don't say what part of the code is a problem. We obviously can't explain every single line of code, that'd be the equivalent of teaching you matlab.
The code defines three methods of the class, a constructor that construct the object by reading it from a .mat file or if that fails, by calling the LoadFromTir. The loadFromTir function reads the object properties by parsing them from a .tir text file. Finally there is a function to save the object to a .mat file.
Other than the regular expression there's nothing particularly advanced in the code. Even the regular expression is simple. It just tells you whether there's any character that is not an uppercase letter in the string.
agnes jerusha
on 22 Oct 2019
lp1=(model.prior(1));
for j=1:size(data,2)
if model.likelihood(1).feature{j}(data(j))~=-Inf
lp1=lp1+(model.likelihood(1).feature{j}(data(j)));
else
class=2;
return;
end
end
%calc lp0
lp0=(model.prior(2));
for j=1:size(data,2)
if model.likelihood(2).feature{j}(data(j))~=-Inf
lp0=lp0+(model.likelihood(2).feature{j}(data(j)));
else
class=1;
return;
end
end
1 Comment
Steven Lord
on 22 Oct 2019
This isn't related to the main question. Please ask it as a separate question (with an actual question, not simply a block of code.)
WAN NOR NAZIRA MUSTAPA KAMAL
on 19 Jan 2021
Edited: per isakson
on 19 Jan 2021
Hi all can anyone help me to understand this code?
%% setup
hold all
a = arduino('COM3', 'Uno');
mpu = i2cdev(a,'0x68'); %mpu adress is normally 0x68
writeRegister(mpu, hex2dec('B6'), hex2dec('00'), 'int16'); %reset
data = zeros(10000,14,'int8'); %prelocating for the speed
j = 1;
a1 = animatedline('Color',[1 0 0]);
a2 = animatedline('Color',[0 1 0]);
a3 = animatedline('Color',[0 0 1]);
legend('Accel_x','Accel_y','Accel_z')
%% loop
while(true)
x=1;
for i=59:72 % 14 Data Registers for Accel,Temp,Gyro
data(j,x) = readRegister(mpu, i, 'int8');
x = x + 1;
end
y = swapbytes(typecast(data(j,:), 'int16')) %if your system is big-endian remove the swapbytes function
addpoints(a1,j,double(y(1)));
addpoints(a2,j,double(y(2)));
addpoints(a3,j,double(y(3)));
j = j+1;
drawnow limitrate
end
1 Comment
Steven Lord
on 19 Jan 2021
This isn't related to the main question. Please ask it as a separate question.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!