How can I convert this code to C++?

1 view (last 30 days)
Kun Cheng
Kun Cheng on 1 Feb 2024
Commented: Dyuman Joshi on 1 Feb 2024
data=readtable("240201_100952.csv");
%Delete not intresting data
keep=all(data{:,:}> -90000,2);
filterdata = data(keep,:);
time=filterdata(:,9);
high=filterdata(:,10);
second=time{:,1} ./1000;
high_mm=high{:,1} ./1000;
relativetime=cumsum(second);
%Input motor speed(rpm)
constantspeed=input('Enter the constant speed of the motor:');
pr=0.0128 /2;%Pitch Radius(m)
speed=constantspeed* (2 * pi * pr *1000) / 60;%Unit: mm/s
new_x=relativetime*speed;
%Find polyfit
x = new_x;
y = high_mm;
n = 1;
fit = polyfit(x, y, n);
[p,S,mu]=polyfit(x, y, n);
y_error=polyval(p,x,S,mu);
[y_fit,delta] = polyval(p,x,S,mu);
%Determind the D
t=-( fit(1)*x - y + fit(2)) ./ (fit(1)^2 + 1);
[max_t, idx] = max(t);
[min_t, idy] = min(t);
abs_max_t=abs(max(t));
% 找到对应的点
max_t_x = x(idx);
max_t_y = y(idx);
min_t_x = x(idy);
min_t_y = y(idy);
d=(max_t - min_t)*(sqrt(fit(1)^2 + 1^2));
d_final=d ./2;
%Print
print_Straightness=sprintf('Straightness: %s',d_final);
print_Max=sprintf('Max: %s',abs_max_t);
display(print_Straightness);
display(print_Max);
%Draw figure
figure;
xp=x(1):0.1:x(end);
yp= fit(1)*xp + fit(2);
plot(x,y,'o') ;
hold on
plot(xp,yp,'Color','blue','LineStyle','--');
plot(x,y_fit+2*delta,'m--',x,y_fit-2*delta,'m--')
plot(max_t_x ,max_t_y,'o-','Color','black');
plot(min_t_x ,min_t_y,'o-','Color','red')
hold off
xlabel('Distance(mm)');
ylabel('Different High(mm)');
legend('data','linear fit','Prediction interval')

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!