Script that scans through a text file line by line and finds lines that I specify and edits the number values in those line and rewrites it into a copy of the original file.

11 次查看(过去 30 天)
So I have already started on this script. I want to make a script that opens a g-code file and then creates a modified version of this file called "modified (name of original file).gcode". Once matlab has opened the G-code file I want to it to read every line of the file and determine which lines start G0 and G1 and have X Y E values. I use regular expressions to do this. The original G-code file only has X and Y direction parameters so I have my script insert a Z parameter using "InsertBefore". Right now this Z value is just some arbritary value that is added to every line that matches the regular expressions format. I want to change the X Y Z and E values in all of the G0 and G1 lines by 0.2 percent of the original value.
Currently I have it add 0.2% of the original to the original but I would prefer if it could add or subtract randomly, just alter the value in any direction by 0.2%.
I want this script to also do two things in addition to what it already does, which is read, modify, and rewrite lines.
I want it to look at only G0 and G1 lines between blocks of lines that I specify (ie. modify only lines through 20-80). This is because in the original g-code file "dogbone (centered)" there are chunks of lines I want to look at specifically like the lines that follow ;MESH: and ;TYPE:WALL-INNER.
For the Z-value it adds, is there any way where I can apply the same 0.2% changing between the Z values in every line that has it?
I have attatched my script and the dogbone(centered) text file.

回答(1 个)

Ravi
Ravi 2023-12-15
Hi Ashaya KC,
I see there are three queries to be solved here.
  1. Adding or subtracting to the variables at random.
  2. Processing the data between specified lines.
  3. Maintaining consistency in adding or subtracting to the “Z” variable across the file.
Please find the potential solution for your query here.
  • Consider a variable “randomSign” that would determine which operation to perform. If the variable takes a value 1, then perform addition operation, else perform subtraction operation.
randomSign = randi([0, 1]);
if randomSign == 1
% Add
vec = vec + percent * numel(vec);
else
% Subtract
vec = vec - percent * numel(vec);
end
  • To process the file only between specified lines, let us maintain a variable “lineNumber”, and two other variables “startLine” and “endLine” that determine the limits of the file to be processed.
lineNumber = 0;
while ~feof(fid)
lineNumber = lineNumber + 1;
if lineNumber < startLine || lineNumber > endLine
continue;
end
% remaining code
end
  • To maintain consistency in addition or subtraction for “Z” variable across the file, create a variable “zSign” outside the while loop that assumes a value 0 or a 1 and is not updated throughout the file.
zSign = randi([0,1]);
while ~feof(fid)
zvalue = vec(3);
% remaining code
if zSign == 1
% add
zvalue = zvalue + percent * zvalue;
else
% subtract
zvalue = zvalue - percent * zvalue;
end
vec(3) = zvalue;
end
To know more about the “randi” function, refer to the following link.
Hope this solution helps.
Thanks,
Ravi Chandra.
  1 个评论
Steven Lord
Steven Lord 2023-12-15
Consider a variable “randomSign” that would determine which operation to perform.
IMO there's no need to use a supplemental variable here. The documentation page for the rand function includes an example showing how to create uniformly generated pseudorandom numbers in a particular range. In fact, that example generates random numbers between -5 and 5. I'd use that approach to generate the percentage.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by