Hello,
I have a G-code file as you can see in the following structure :
How I can with matlab to find the number of rows which carry the character of Z and to copy the instructions between two defined rows in another file .txt.
Thank you

댓글 수: 2

Rik
Rik 2020년 6월 8일
It sounds like you can just read this file as text and use contains to look for a Z.
K. Taieb
K. Taieb 2020년 6월 8일
Thank you Rik
I think contains returns a logical value. I want to find his position. My goal is to copy the instructions after the line that contains z.
str = fileread('gcode.txt');
caracter = "Z";
TF = contains(str,caracter)

댓글을 달려면 로그인하십시오.

 채택된 답변

Rik
Rik 2020년 6월 8일
편집: Rik 2020년 6월 8일

1 개 추천

str = fileread('gcode.txt');
cell_str=strsplit(str,'\n')
character = 'Z';
TF = contains(cell_str,character);
line_num=find(TF);

댓글 수: 6

K. Taieb
K. Taieb 2020년 6월 8일
Thank you! That's exactly what I am looking for.
How I can copy the instructions that are between the two lines in another file.
Rik
Rik 2020년 6월 8일
Step by step: once you have the 2 line numbers that contain a Z, how can you generate the list of numbers in between? The colon operator should help you here.
Now that you have the data you want to put in your other file: how can you write data to file? That must be one of the most asked questions for Matlab.
K. Taieb
K. Taieb 2020년 6월 8일
In the previous example, line_num = 1, 12
The idea is to concatenate the lines 2,3,4,5,6,7,8,9,10,11
but in the new file, we will have 10 lignes
Rik
Rik 2020년 6월 8일
That simply means you need to select your 10 lines from your current data:
new_txt=cell_str( (line_num(1)+1):(line_num(2)-1) );
Now you can print that text to your new file.
K. Taieb
K. Taieb 2020년 6월 8일
Thank you!
Rik
Rik 2020년 6월 8일
You're welcome. If you feel my answer solved your question, please mark it as accepted answer. If not, feel free to comment with your remaining issues.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2020년 6월 8일

1 개 추천

Maybe the easiest way it to loop through the text line-by-line and check if it contains the character 'Z' using strfind or findstr. Then it should be perfectly straightforward to keep the indices where you find your Zs.
HTH

카테고리

도움말 센터File Exchange에서 Language Support에 대해 자세히 알아보기

태그

질문:

2020년 6월 8일

댓글:

Rik
2020년 6월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by