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
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2019년 3월 28일
편집: Jan 2019년 3월 28일

0 개 추천

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.
For plotting - replace
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
by
match = (g == 1);
m1 = [match(1:end-1); false]; % [EDITED, typo fixed: , -> ;]
m2 = match;
m2(1) = false;
plot3([x(m1), x(m2)], [y(m1), y(m2)], [z(m1), z(m2)], 'LineWidth', 0.8, 'color', 'r');
Does this work? I cannot try it due to the lack of input data.

댓글 수: 3

It gives the folowing error message:
This code should be usabe with any .gcode file.
% Error Message
Error using horzcat
Dimensions of arrays being concatenated are not
consistent.
Error in helptest (line 62)
m1 = [match(1:end-1), false];
% Code
% fid = fopen('VK_Holllow_Pyramid.gcode');
clear
tic
fid = textread('VK_Top_Hat.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
end
end
end
end
end
%%
toc
z = laura(:,1);
x = laura(:,2);
y = laura(:,3);
g = laura(:,4);
% fig = figure('visible','off');
match = (g == 1);
m1 = [match(1:end-1), false];
m2 = match;
m2(1) = false;
plot3([x(m1), x(m2)], [y(m1), y(m2)], [z(m1), z(m2)], 'LineWidth', 0.8, 'color', 'r');
% set(gca,'visible','off')
% set(gca, 'Projection','perspective');
% set(gca, 'CameraPosition', [100 0 0]);
% set(gca, 'CameraPosition', [100 0]);
NoLayers = max(z(:,1));
figure(fig)
hold off
To answer your question on how I know plotting takes more time, I have put a timer on each section to work out where the inefficiencies are
I do not have any .gcode files. Do you want me to search some in the internet?
This line contained a typo:
m1 = [match(1:end-1), false];
% ^ must be a ;
I assume, you can fix this by your own.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

질문:

2019년 3월 28일

편집:

Jan
2019년 3월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by