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

14 次查看(过去 30 天)
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 4x1 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!

采纳的回答

Steven Lord
Steven Lord 2016-11-10
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}];
  1 个评论
Jklaay
Jklaay 2016-11-11
Thank you, this worked well. Below is my final-ish code. Now just to get the coordinates from the printer and replace the 'G(x)'s!
entries = {' G0 '; % Command associated with 0
' G1 ';
' G2 ';
' G3 ';
' G4 ';
' G5 '; % Command associated with 5
' G6 ';
' G7 ';
' G8 ';
' G9 '}; % Command associated with 9
x=1;
for combination = 1:9999
number = sprintf('%04d', combination) - '0';
instruction(x,1) = cellstr([entries{number+1}]);
x=x+1;
end

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2016-11-10
gcode = regexprep(celldata, {zero, one, two, three, four, five, six, seven, eight, nine}, {zeror, oner, twor, threer, fourr, fiver, sixr, sevenr, eightr, niner});

Eslam Sileem
Eslam Sileem 2019-8-7
gcode = regexprep(celldata, {zero, one, two, three, four, five, six, seven, eight, nine}, {zeror, oner, twor, threer, fourr, fiver, sixr, sevenr, eightr, niner});

类别

Help CenterFile Exchange 中查找有关 Language Support 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by