Dear all, i'm dealing with a problem: I have a .txt table and i want to write and delete the last n rows of this table using a condition or statement (here named solution).
here is the .txt table (shorelines.txt):
Station Profile_number shoreline_number east north distance_along_swath shoreline_elevation error analysis_type time
CLMO 5 1 681775 5955821 101.18 11.67 0.98 classic 735513.08088
CLMO 7 1 681295 5955779 197.57 50.48 4.15 classic 735513.08133
CLMO 7 2 681350 5955683 86.25 19.54 0.96 classic 735513.08154
CLMO 9 1 681116 5955539 98.12 21.44 0.67 classic 735513.08208
CLMO 9 2 681114 5955540 100.87 24.03 1.21 classic 735513.08231
CLMO 10 1 681073 5955484 82.87 19.94 0.62 stack 735513.08275
First I readed the table in this way:
clear
%load shorelines table
file=('shorelines.txt');
fid=fopen(file,'r');
i=1;
while ~feof(fid)
%tline = fgetl(fid);
A=textscan(fid,'%s %u %u %f %f %f %f %f %s %f\r\n','delimiter',' ','HeaderLines',1);
for j=1:10 %number of columns (default)
data(i,j) = A(1,j);
end
i=i+1;
end
clear j i
then I create the conditions defined as:
solution==1 %delete the last three rows of the table
solution==2 %delete the two last rows of the table
solution==3 %delete just the last row of the table
i've been trying to develop a method to delete the rows but none works. For instance, i try this:
if solution==3
tline(nim)=fgetl(nim) ;
fclose(fid);
end
but nothing happens... somebody have an idea how to deal with .txt tables? thanks a lot in advance...

4 comentarios

Cedric
Cedric el 7 de Oct. de 2013
You want to delete rows in the original text file, or in this array data that you are building?
Jules Ray
Jules Ray el 7 de Oct. de 2013
Hi Cedric, i want to delete rows in the original table shorelines.txt...
Cedric
Cedric el 7 de Oct. de 2013
So you want to be able to define a file name and a set of numbers of lines, e.g. 'shorelines.txt' and [1,2,3,5], and have as an output four other files, named e.g. 'shorelines_1.txt', shorelines_2.txt, shorelines_3.txt, shorelines_5.txt, where the last [1,2,3,5] lines were removed? And you don't want to make any computation with these data at his point, it' really just about building files .. ?
Jules Ray
Jules Ray el 7 de Oct. de 2013
yes, i want to read the alreay existent file.txt, but i want to edit the file removing the bottom lines (depending of the case i need to remove one, two or three lines from the lower part). I dont need to split the file. And i dont want to do any computation, just remove some lines from the lower part of the txt. Thanks.

Iniciar sesión para comentar.

 Respuesta aceptada

Cedric
Cedric el 7 de Oct. de 2013
Editada: Cedric el 7 de Oct. de 2013

0 votos

Here is a solution assuming that my comment above under your question was correct.
% - Define run parameters.
fileLocator = 'shorelines.txt' ;
headerlines = 1 ;
nList = [1,2,5,20] ;
% - Split input file name, read file.
[path_in,fname_in,ext_in] = fileparts( fileLocator ) ;
buffer = fileread( fileLocator ) ;
% - Find lines ends (must check wheter file ends with one and remove
% it from the list if present).
lineEnds = find( buffer == 10 ) ;
if buffer(end) == 10 || buffer(end-1) == 10
lineEnds(end) = [] ;
end
% - Loop over n's.
for k = 1 : numel( nList ) ;
% Check not case where more lines to remove than present in file.
if numel(lineEnds)-nList(k) < 1
fprintf( 'Skip removing %d lines, file contains only %d lines.\n', ...
nList(k), numel(lineEnds) ) ;
continue ;
end
% Compute cutoff position in buffer.
cutoff = lineEnds(end-nList(k)+1)-1 ;
% Build new file name.
fname_out = sprintf( '%s_%d.%s', fname_in, nList(k), ext_in ) ;
% Output.
fid = fopen( fullfile( path_in, fname_out ), 'w' ) ;
fwrite( fid, buffer(1:cutoff) ) ;
fclose( fid ) ;
end
Note that this code is just to illustrate. To make it robust, you'll have to take in account the number of header lines in the check that the number of lines to remove doesn't exceed the number of data lines, etc.

4 comentarios

Jules Ray
Jules Ray el 7 de Oct. de 2013
Thanks a lot Cedric, the part of the cutoff is the keystone i think, but there are some confusion about what i want to obtain. I will show you:
This is the original table (shorelines.txt):
Station Profile_number shoreline_number east north distance_along_swath shoreline_elevation error analysis_type time
CLMO 5 1 681775 5955821 101.18 11.67 0.98 classic 735513.08088
CLMO 7 1 681295 5955779 197.57 50.48 4.15 classic 735513.08133
CLMO 7 2 681350 5955683 86.25 19.54 0.96 classic 735513.08154
CLMO 9 1 681116 5955539 98.12 21.44 0.67 classic 735513.08208
CLMO 9 2 681114 5955540 100.87 24.03 1.21 classic 735513.08231
CLMO 10 1 681073 5955484 82.87 19.94 0.62 stack 735513.08275
I define three cases (solution==1, solution==2,solution==3)
for example if I use the condition solution==3, that means that i need to delete just one row of the table and keep the rest. So the result will be:
Station Profile_number shoreline_number east north distance_along_swath shoreline_elevation error analysis_type time
CLMO 5 1 681775 5955821 101.18 11.67 0.98 classic 735513.08088
CLMO 7 1 681295 5955779 197.57 50.48 4.15 classic 735513.08133
CLMO 7 2 681350 5955683 86.25 19.54 0.96 classic 735513.08154
CLMO 9 1 681116 5955539 98.12 21.44 0.67 classic 735513.08208
CLMO 9 2 681114 5955540 100.87 24.03 1.21 classic 735513.08231
another example, if i choose the condition solution==1, that means that i need to delete 3 rows and the output will be:
Station Profile_number shoreline_number east north distance_along_swath shoreline_elevation error analysis_type time
CLMO 5 1 681775 5955821 101.18 11.67 0.98 classic 735513.08088
CLMO 7 1 681295 5955779 197.57 50.48 4.15 classic 735513.08133
CLMO 7 2 681350 5955683 86.25 19.54 0.96 classic 735513.08154
is litle bit more simple that your answer, because i dont need to dismember the table, just remove rows according the condition keeping the table.
Thanks a lot for your help.
Cedric
Cedric el 7 de Oct. de 2013
Editada: Cedric el 7 de Oct. de 2013
Hi Jules, what you describe is roughly what I implemented. I just implemented a loop over a set of numbers of lines (so the file is read only once if you want to export versions cut at different places), but this is the only difference. It looks complicated because, as Walter explains, there is no simple way to delete "the n last rows" of a file. We are in fact left with reading the file line by line and outputting only the relevant lines, or to read the file in one shot and cut it based on where we find "new line" special characters (which is what I implemented). If you need to export only one version of the file, then you can forget about the loop and implement something like:
fileLocator = 'shorelines.txt' ;
solution = .. ; % User defined.
switch solution
case 1
n = 4 ;
case 2
n = 3 ;
case 3
n = 1 ;
end
wasOk = cutFile( fileLocator, n ) ;
if ~wasOk
error( 'CUTFILE failed for some reason!' ) ;
end
where function CUTFILE is defined as
function isOk = cutfile( fileLocator, nLines )
isOk = true ;
% - Split input file name, read file.
[path_in,fname_in,ext_in] = fileparts( fileLocator ) ;
buffer = fileread( fileLocator ) ;
% - Find lines ends (must check wheter file ends with one and remove
% it from the list if present).
lineEnds = find( buffer == 10 ) ;
if buffer(end) == 10 || buffer(end-1) == 10
lineEnds(end) = [] ;
end
% - Check not case where more lines to remove than present in file.
if numel(lineEnds)-nLines < 1
fprintf( 'Skip removing %d lines, file contains only %d lines.\n', ...
nLines, numel(lineEnds) ) ;
isOk = false ;
return ;
end
% - Compute cutoff position in buffer.
cutoff = lineEnds(end-nLines+1)-1 ;
% - Build new file name.
fname_out = sprintf( '%s_%d.%s', fname_in, nLines, ext_in ) ;
% - Output to file.
fid = fopen( fullfile( path_in, fname_out ), 'w' ) ;
fwrite( fid, buffer(1:cutoff) ) ;
fclose( fid ) ;
end
Jules Ray
Jules Ray el 8 de Oct. de 2013
wow, that's it...
thanks a lot Cedric
Cedric
Cedric el 8 de Oct. de 2013
You're welcome.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 7 de Oct. de 2013

0 votos

It is not possible in MATLAB to make a file shorter (except to remove everything in it) -- at least not without adding some MEX.
You should copy the parts you want to keep into another file. Afterwards you might want to rename the new file to the old.

2 comentarios

Jules Ray
Jules Ray el 7 de Oct. de 2013
Editada: Jules Ray el 7 de Oct. de 2013
Hello Walter: do you know some .mex that can do this? this is part of GUI, in fact is the main output. Maybe another format could work (maybe excell, .csv, etc.), any idea? Thanks in advance
Walter Roberson
Walter Roberson el 7 de Oct. de 2013
Even with a .mex you would need to arrange your code so that it rewrote the entire remaining part of the file from the point that the first bit of it was deleted; and then when you got to where the new file end should be you would have the code call ftrunc() or trunc()... if you can figure out the file identifier.
None of the operating systems that MATLAB currently runs on provide a "delete line" I/O call. MATLAB file handling has always been straight sequential stream access; MATLAB was not able to use the VAX/VMS RMS (Record Management System) call for this purpose even when MATLAB ran on VMS.

Iniciar sesión para comentar.

Productos

Etiquetas

Preguntada:

el 7 de Oct. de 2013

Comentada:

el 8 de Oct. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by