Increase the Efficiency of Plotting G-code
显示 更早的评论
Hi,
I have written a code that reads a .gcode file, finds the x, y and z values and then plots a graph so you can see the part however, its quite slow to run and was hoping on some ideas on how I could speed it up.
I think its the part where I specifiy to only draw a line if the previous g-code line is a G1 (which is an instruction to extrude in g-code), I have highlighted this section in bold.
Any help would be great. I have attached the code below.
%%
% fid = fopen('VK_Holllow_Pyramid.gcode');
clear
tic
fid = textread('VK_Holllow_Pyramid.gcode','%s','delimiter',';');
lineNumLaura = 1;
xVal = 0;
yVal = 0;
fVal = 0;
gVal = 0;
layerNum = -1;
for lineNum = 1:size(fid,1)
v = strsplit(fid{lineNum},' ');
if regexp((v{1}),'LAYER:*') %Find layer number
layerNum = layerNum + 1;
end
% check if first value in 'G' to mark start of data
if regexp((v{1}),'G*')
if strcmp(v{1}(2),'0') || strcmp(v{1}(2),'1')
laura(lineNumLaura,4) = str2double(v{1}(2));
for i = 2:size(v,2)
if ~isempty(regexp((v{i}),'X*')) || ~isempty(regexp((v{i}),'Y*'));
laura(lineNumLaura,1) = layerNum;
if regexp((v{i}),'X*')
xVal = v{i};
laura(lineNumLaura,2) = str2double(xVal(2:end));
end
if regexp((v{i}),'Y*')
yVal = v{i};
laura(lineNumLaura,3) = str2double(yVal(2:end));
lineNumLaura = lineNumLaura + 1;
end
% if regexp((v{i}),'F*')
% fVal = v{i};
% laura(lineNumLaura,4) = str2double(fVal(2:end));
% lineNumLaura = lineNumLaura + 1;
% end
end
end
end
end
end
%%
toc
z = laura(:,1);
x = laura(:,2);
y = laura(:,3);
g = laura(:,4);
fig = figure('visible','off');
for i = 2:numel(z)
if g(i-1) == 1
plot3([x(i-1), x(i)], [y(i-1), y(i)], [z(i-1), z(i)],'LineWidth',0.8, 'color', 'r')
hold on
end
end
set(gca,'visible','off')
% set(gca, 'Projection','perspective');
set(gca, 'CameraPosition', [100 0 0]);
NoLayers = max(z(:,1));
figure(fig)
hold off
1 个评论
Jan
2019-3-28
Please format your code using the buttons on top of the edit field. Then the code is easier to read.
Why do you assume, that the plotting consumes more time than the reading of the file?
regexp((v{1}),'LAYER:*') is much slower than strncmp(v{1}, 'LAYER:', 6).
You execute (regexp((v{i}),'X*') twice, which is a waste of time.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Nearest Neighbors 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!