How to edit a text file using matlab?

3 visualizaciones (últimos 30 días)
Tony Morkos
Tony Morkos el 12 de Sept. de 2017
Editada: Cedric el 14 de Sept. de 2017
I have a text file with time and coordinates. The coordinates are in inches and i need to change it to metres. How can I do this? I have imported the data from matlab and tried doing this but it gave me an error for X:
% Read txt into cell A
fid = fopen('Sample.txt','r');
i = 1;
tline = fgetl(fid);
A{i} = tline;
while ischar(tline)
i = i+1;
tline = fgetl(fid);
A{i} = tline;
end
fclose(fid);
%Change inches to m
%Change Cell B
A{X/Ycoords} = convlength(X/Ycoords,'in','m');
A sample of the text file is this:
Initial Velocity
m/s
57.148684
Time(sec): 0.000000
X/Ycoords (in): 0.000000000/0.000000000
_______________
Time(sec): 0.020000
X/Ycoords (in): 45.064658630/0.000000000
_______________
Time(sec): 0.040000
X/Ycoords (in): 90.174230200/0.000000000
_______________
Time(sec): 0.060000
X/Ycoords (in): 135.306654500/0.000000000
Your help is much appreciated. Thanks

Respuestas (2)

Reece Teramoto
Reece Teramoto el 14 de Sept. de 2017
The error for X that you are getting comes from this line:
A{X/Ycoords}
At this point in your program, A is just a cell array where each cell is one line of the input file. The variables 'X' and 'Ycoords' are not defined, so you cannot index into A with them.
When you import the data from the text file into MATLAB, you are reading each line of the file into its own cell in a cell array:
A{i} = tline;
This means that when you get to the line in the file
'X/Ycoords (in): 45.064658630/0.000000000'
this entire line is put into one cell as a string. I suggest performing additional parsing on the input file to break up the lines, perhaps using the 'strsplit' function.
Using 'strsplit', you could split the line further before putting it into the cell array. Consider this example:
s = 'X/Ycoords (in): 45.064658630/0.000000000';
s_after_split = strsplit(s, {' ','(in):','/'}, 'CollapseDelimiters', true)
Result:
s_after_split =
1×4 cell array
'X' 'Ycoords' '45.064658630' '0.000000000'
From here, since the x and y values are stored in cells of their own, we can more easily convert them from inches to meters:
% convert the x and y values from strings to doubles
s_after_split(3:4) = num2cell(str2double(s_after_split(3:4)));
% convert the x value from inches to meters
s_after_split{3} = convlength(s_after_split{3},'in','m');
% convert the y value from inches to meters
s_after_split{4} = convlength(s_after_split{4},'in','m')
Result:
s_after_split =
1×4 cell array
'X' 'Ycoords' [1.1446] [0]

Cedric
Cedric el 14 de Sept. de 2017
Editada: Cedric el 14 de Sept. de 2017
Here is an alternate solution based on the fact that you don't seem to have special characters/sequences in your file content that are interpreted in a formatSpec:
content = fileread( 'MyFile.txt' ) ;
xy = regexp( content, '([\d\.]+)/([\d\.]+)', 'tokens' ) ;
xy = 0.0254 * str2double( horzcat( xy{:} )) ;
content = regexprep( content, 'in\): \S+', 'm\): %f/%f' ) ;
fId = fopen( 'MyFile_meter.txt', 'w' ) ;
fprintf( fId, content, xy ) ;
fclose( fId ) ;
PS: It is not something that I recommend as a sound/stable approach (*), but if you just needed a few lines of code that do the trick for a one-shot conversion operation, that should work.
(*) because it is kind of a trick: we use the content as a formatSpec for the output after replacing former numbers by '%f'.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by