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

14 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Steven Lord
Steven Lord il 10 Nov 2016
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 Commento
Jklaay
Jklaay il 11 Nov 2016
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

Accedi per commentare.

Più risposte (2)

Walter Roberson
Walter Roberson il 10 Nov 2016
gcode = regexprep(celldata, {zero, one, two, three, four, five, six, seven, eight, nine}, {zeror, oner, twor, threer, fourr, fiver, sixr, sevenr, eightr, niner});
  1 Commento
Jklaay
Jklaay il 10 Nov 2016
Unfortunately, this gave the same output. Thanks though! If nothing else the code looks a lot nicer!

Accedi per commentare.


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

Categorie

Scopri di più su Language Support in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by