Add lines of text to CNC programming code

Hello, I am trying to generate a code in which after a tool change (depending on the tool) Matlab puts a new line of code with an M7 or M8, for example, within a bunch of text I have this:
N80T16M6
I want Matlab to create a new line with a M7 and M8 like this
N80T16M6
M7
M8
It varies depending on the number after the T (which is the number of the tool) for example T15 would just have M7. Is there a way of programming this? Adding the information of which tool needs what?
Besides this, I want to include in a certain point of the text the stock dimensions, for example (x10::y-5::z-0.1::) just as I wrote it with the parenthesis
and as extra information as I mentioned before this lines of code are within a bunch of lines but always starts with an N number, like this:
Thanks a lot in advance!

Answers (1)

filename_in = '10267.TAP';
[filedir, basename, ext] = fileparts(filename_in);
filename_out = fullfile(filedir, basename + "_out" + ext);
S = readlines(filename_in);
p15 = find(startswith(S, 'N80T15'));
for K = flipud(p15).'
S = [S(1:K); "M7";S(K+1:end)];
end
p16 = startswith(S, 'N80T16');
for K = flipud(p16).'
S = [S(1:K); "M7"; "M8"; S(K+1:end)];
end
writelines(S, filename_out);

7 Comments

I am sorry but I am just a beginner, can you explain me how to use the code properly? thanks a lot
Change filename_in to the file name of the input file.
Run the code.
The output will be the same filename but with "_out" at the end of it.
The way the code works is that it locates all of the lines that start with N80T15, and loops in reverse order inserting 'M7' lines after each one of them. Then it locates all of the lines that start with N80T16 and loops in reverse order inserting M7 and M8 lines after each of them. Looping in reverse order saves having to adjust the positions to account for the inserted lines.
one of the files that need this changes is attached to this message (1.TXT), it's just one example, The real file uses a .NCF extension, the code returns the 1_out file (with .NCF extension) but as you may see there is a lot of M8 and M7 at the code beggining, can you help me solving this? really appreciated
filename_in = '1.TXT';
[filedir, basename, ext] = fileparts(filename_in);
filename_out = fullfile(filedir, basename + "_out" + ext);
S = readlines(filename_in);
p15 = find(startsWith(S, 'N80T15'));
for K = flipud(p15).'
S = [S(1:K); "M7";S(K+1:end)];
end
p16 = find(startsWith(S, 'N80T16'));
for K = flipud(p16).'
S = [S(1:K); "M7"; "M8"; S(K+1:end)];
end
writelines(S, filename_out);
The result is the same file, however, as there are no lines that start with N80T16 or N80T15.
... If you want (for example) N160T16M6 to be affected, then you need to specify the rules for when the text is to be added.
Understood, what happens is that I know for sure the text always starts with an N(random number), contains a "T(tool number)" and an "M6", but as you can see the rest of the text changes:
N160T16M6( T16 = 0.5IN SPOTDRILL / SCRIBE )
is there any way to include it in the code?
I know I have said this a lot, but I truly appreciate it, Thanks!
can you help me adding this features please?
filename_in = '1.TXT';
[filedir, basename, ext] = fileparts(filename_in);
filename_out = fullfile(filedir, basename + "_out" + ext);
S = readlines(filename_in);
M6 = find(regexp(S, '^N\d+T\d+M6'));
for K = flipud(M6).'
S = [S(1:K); "M7";S(K+1:end)];
end
writelines(S, filename_out);

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!