Text-file manipulation

I would like to know, if there is a way to manipulate a text file? I have a text file looks like the following:
Befehl\3-Axis\Form-R\NC_YXZ_CenterofMasse.TXT }
N20 {Info Tool---> Diameter1 [mm] = 10.000000}
N30 {Info Tool---> Length2 [mm] = 55.000000}
N40 {Info Tool---> Diameter2 [mm] = 6.000000}
N50 {Info Tool---> Length1 [mm] = 15.000000}
N60 {Info Feed [mm/min] = 1000}
N80 {linear Interpolation}
N90 G49 G54 G00 F1500.00 X 42.50 Y-104.22 Z155.00 A 0.00 B 0.00 C 0.00 M24.250
N100 G00 X 85.00 Y-215.00 Z105.00 A 0.00 B 0.00 C 0.00
N110 G01 X 85.00 Y-217.00 Z 94.25 A 0.00 B 0.00 C 0.00
N120 G01 X 0.00 Y-217.00 Z 94.25 A 0.00 B 0.00 C 0.00
and I want this part of this text-only
X 42.50 Y-104.22 Z155.00 A 0.00 B 0.00 C 0.00
X 85.00 Y-215.00 Z105.00 A 0.00 B 0.00 C 0.00
X 85.00 Y-217.00 Z 94.25 A 0.00 B 0.00 C 0.00
X 0.00 Y-217.00 Z 94.25 A 0.00 B 0.00 C 0.00
Copy and store in an array, only the values without X, Y, Z, A, B, C and I later read off each other in two lines and use part of my program, however, can be a vector P1 = [-104.22 42.50 155 0 0 0 ] and P2 = [85 -215 105 0 0 0], etc.
second problem: I show some values into a text field in my Gui, these values are as follows:
X: 1.000 Y: 2.000 Z: 3.000 A: 4.000 B: 5.000 C: 6.000 A1: 1000 A2: 2,000 A3: 3,000 A4: 4,000 A5: A6 5000: 6,000
and I want to know if I can manipulate reported that line and can get into this shape?
P1 = [1 2 3 4 5 6]
Thanks in advance January

1 commentaire

Jan
Jan le 13 Nov 2011
Hi Jan, if you post your question in different forums, adding a link to the other forum is helpful for the readers.

Connectez-vous pour commenter.

Réponses (3)

Daniel Shub
Daniel Shub le 13 Nov 2011

0 votes

This smells like a regular expression to me. I am guessing someone with good regexp foo can do this with one line. I am not that person. You might have better luck someplace like: http://regexadvice.com/forums/
Jan
Jan le 10 Déc 2011

0 votes

Hi,
I've solved the second problem. But the first problem yet. Can someone help me? The solution for the second problem:
result=X: 1.000 Y: 2.000 Z: 3.000 A: 4.000 B: 5.000 C: 6.000 A1: 1000 A2: 2,000 A3: 3,000 A4: 4,000 A5: A6 5000: 6,000 b=regexprep(result,'(X:)|(Y:)|(Z:)|(A:)|(B:)|(C:)|(A2:)|(A3:)|(A4:)|(A5:)|(A6:)',' ') neu = b(1 : findstr(b,'A1') - 1); % Delete last 6 values and store into new! P1=neu;
Thanks Jan

1 commentaire

Walter Roberson
Walter Roberson le 10 Déc 2011
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 11 Déc 2011

0 votes

Untested:
fid = fopen('YourTextFile.txt', 'rt');
datacell = textscan(fid, '%f%*cY%f%*cZ%f%*cZ%f%*cA%f%*cB%f%*cC%f', 'HeaderLines', 7, 'CommentStyle', {'N', 'X'}, 'CollectOutput', 1);
fclose(fid);
result = datacell{1};
After which the various vectors you want are result(1,:), result(2,:) and so on.
I am using a bit of a trick here, telling it that anything starting with N is a comment until (and including) the next X. This will handle both the case where you have multiple G fields before the X, and the case where you have a single G field before the X: the N through X will just be thrown out. Because the X is thrown out in that process, I do not include it in the format.

Catégories

En savoir plus sur Debugging and Analysis dans Centre d'aide et File Exchange

Tags

Question posée :

Jan
le 13 Nov 2011

Community Treasure Hunt

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

Start Hunting!

Translated by