MATLAB: Writing Gcode with Matlab; How to replace numbers with lines of text

3dprintergcodeMATLAB

After three years, I forgot the 4 digit passcode to my iphone 5. Luckly, the phone is jailbroken and has no lockout/limited attempts. After entering 0000-1000, I got bored and made a plan to use a stylus attached to my 3d printer to punch all the possible combinations into my phone while a webcam records. In my code below, I attempt to convert all possible combinations to gcode, replacing each number with a specified command/location for the printer.
The problem I have is once the code replaces all the '1's with the line of gcode, the '2' search finds numbers within those lines of gcode and replaces the '2's there too… this results in a jumbled mess.
x=1;
while x<10000
data=num2str(x, '%04.f');
celldata(x,1)= cellstr(data);
x=x+1;
end
%name = # to search for
%namer = gcode line to replace 'name'
zero = '0';
zeror = ' G1 X231.599 Y94.103 Z0 E0 ';
one = '1';
oner = ' G1 X231.599 Y94.103 Z0 E0 ';
two = '2';
twor = ' G1 X231.599 Y94.103 Z0 E0 ';
three = '3';
threer = ' G1 X231.599 Y94.103 Z0 E0 ';
four = '4';
fourr = ' G1 X231.599 Y94.103 Z0 E0 ';
five = '5';
fiver = ' G1 X231.599 Y94.103 Z0 E0 ';
six = '6';
sixr = ' G1 X231.599 Y94.103 Z0 E0 ';
seven = '7';
sevenr = ' G1 X231.599 Y94.103 Z0 E0 ';
eight = '8';
eightr = ' G1 X231.599 Y94.103 Z0 E0 ';
nine = '9';
niner = ' G1 X231.599 Y94.103 Z0 E0 ';
gcode = regexprep(celldata,zero,zeror);
gcode = regexprep(gcode,one,oner);
gcode = regexprep(gcode,two,twor);
gcode = regexprep(gcode,three,threer);
gcode = regexprep(gcode,four,fourr);
gcode = regexprep(gcode,five,fiver);
gcode = regexprep(gcode,six,sixr);
gcode = regexprep(gcode,seven,sevenr);
gcode = regexprep(gcode,eight,eightr);
gcode = regexprep(gcode,nine,niner);
%Example Line: G1 X231.599 Y94.103 E5.0867
Is there a way I can make the script differentiate between the lines of gcode and the numbers in the combinations?
I also have tried the code below in an attempt to make the numbers in the cells separate so I can potentially make a loop to address and replace each cell individually. The problem I face is indexing another array when 'str' is a 4×1 double…
x=1000;
while x<10000
str=num2str(x) - '0';
array(1,x)=str
x=x+1;
end
Any and all help its greatly appreciated!

Best Answer

Indexing. I'm going to tweak the elements in entries a bit, changing the first string to G followed by the appropriate digit just for illustration. [I don't know if that's the correct way to handle this in your real application.]
entries = {' G0 X231.599 Y94.103 Z0 E0 '; % Command associated with 0
' G1 X231.599 Y94.103 Z0 E0 ';
' G2 X231.599 Y94.103 Z0 E0 ';
' G3 X231.599 Y94.103 Z0 E0 ';
' G4 X231.599 Y94.103 Z0 E0 ';
' G5 X231.599 Y94.103 Z0 E0 '; % Command associated with 5
' G6 X231.599 Y94.103 Z0 E0 ';
' G7 X231.599 Y94.103 Z0 E0 ';
' G8 X231.599 Y94.103 Z0 E0 ';
' G9 X231.599 Y94.103 Z0 E0 '}; % Command associated with 9
for combination = 1:9999
number = sprintf('%04d', combination) - '0';
instructions = [entries{number+1}];
% Some step involving instructions
end
You can check this:
combination = 623;
number = sprintf('%04d', combination) - '0';
instructions = [entries{number+1}];