Extracting numbers from a G-code file

6 vues (au cours des 30 derniers jours)
Sagar Mehta
Sagar Mehta le 6 Mai 2019
Commenté : Federico Paolucci le 17 Août 2022
Hi!
I want to extract numerical values from a long g-code file of the form:
G1 X-57.028 Y-53.059 E15.60701 ; skirt
G1 X-55.885 Y-55.047 E15.67798 ; skirt
G1 X-54.132 Y-56.526 E15.74894 ; skirt
G1 X-51.980 Y-57.319 E15.81990 ; skirt
G1 X-50.875 Y-57.418 E15.85423 ; skirt
G1 E13.85423 F2400.00000 ; retract extruder 0
G92 E0 ; reset extrusion distance
G1 X-39.700 Y-39.700 F7800.000 ; move to first perimeter point
G1 E2.00000 F2400.00000 ; unretract extruder 0
G1 F1800
G1 X39.700 Y-39.700 E4.45673 ; perimeter
G1 X39.700 Y39.700 E6.91345 ; perimeter
G1 X-39.700 Y39.700 E9.37018 ; perimeter
G1 X-39.700 Y-39.625 E11.82459 ; perimeter
G1 X-39.075 Y-39.075 F7800.000 ;
e
Now, I want to extract only the X Y E values. I.e. for X-57.028 Y-53.059 E15.60701 i want : -57.028 -53.059 15.60701
I would also like to store this in individual coloums (namely X, Y and E) or a matrix that will store all these numerical vales.
Can anyone please suggest a solution ?
Thank you.

Réponses (2)

Stephen23
Stephen23 le 7 Mai 2019
Simple use of one regular expression is all that you need to identify those X, Y, and E values:
str = fileread('test.txt');
rgx = sprintf('\\s+%c([+-]?\\d+\\.?\\d*)','XYE');
tkn = regexp(str,rgx,'tokens');
mat = str2double(vertcat(tkn{:}))
Gving:
mat =
-57.028 -53.059 15.607
-55.885 -55.047 15.678
-54.132 -56.526 15.749
-51.98 -57.319 15.82
-50.875 -57.418 15.854
39.7 -39.7 4.4567
39.7 39.7 6.9135
-39.7 39.7 9.3702
-39.7 -39.625 11.825
  1 commentaire
First Last
First Last le 10 Sep 2021
wow thank you, that helps alot

Connectez-vous pour commenter.


Bhaskar R
Bhaskar R le 7 Mai 2019
I put all your given data as "gcodefile.txt" , hope help you this
matchWords = {'X','Y','E'}; % Your required letters data
fid = fopen('gcodefile.txt', 'r'); % Open file id to read
tline = fgetl(fid); % Get line
counter = 1;
while ischar(tline)
tline = fgetl(fid);
if tline == -1
return;
end
tline(strfind(tline, '-')) = []; % Eliminate - after X,Y,E
% Saerching three character data after X,Y,E and absent of the letter
% indicated the Not a number
for ii = 1:3
[a,b] = regexp(tline,'\d+(\.\d+)?'); % Regulor expression to find matchcase letter
isfind = strfind(tline,matchWords{ii});
if ~isempty(isfind)
strPos = find(a > isfind,1,'first');
val = str2double(tline(a(strPos):b(strPos))); % Get the value upto next character
else
val = NaN; % if not found assign as NaN
end
tmparray(:, ii) = val;
end
Outputarray(counter,:) = tmparray;
counter = counter + 1;
end
Outputarray % First column as X, Second Y, third E
fclose(fid); %Close fid
  2 commentaires
Sagar Mehta
Sagar Mehta le 8 Mai 2019
Thank you Bhaskar!!
Federico Paolucci
Federico Paolucci le 17 Août 2022

Connectez-vous pour commenter.

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by