How to delete file columns?

15 vues (au cours des 30 derniers jours)
Bruno Souza
Bruno Souza le 24 Déc 2017
Commenté : Bruno Souza le 24 Déc 2017
I've the file below, and I would like delete the columns 2, 3 and 4. And If It's possible, put a header file "In Temp" in column 1, and "Time" in column 5.
19.99475 0.00000000 1 1 01.00.00
19.93432 0.00000000 1 1 02.00.00
19.95047 0.00000000 1 1 03.00.00
19.94692 0.00000000 1 1 04.00.00
19.94284 0.00000000 1 1 05.00.00
19.93054 0.00000000 1 1 06.00.00
19.91392 0.00000000 1 1 07.00.00
19.91620 0.00000000 1 1 08.00.00
20.07122 0.00000000 1 1 09.00.00
30.74621 0.00000000 1 1 10.00.00
33.82622 0.00000000 1 1 11.00.00
24.26688 0.00000000 1 1 12.00.00
25.74946 0.00000000 1 1 13.00.00
27.38718 0.00000000 1 1 14.00.00
26.84582 0.00000000 1 1 15.00.00
26.75560 0.00000000 1 1 16.00.00
26.99042 0.00000000 1 1 17.00.00
28.62772 0.00000000 1 1 18.00.00
29.07095 0.00000000 1 1 19.00.00
29.41968 0.00000000 1 1 20.00.00
29.66041 0.00000000 1 1 21.00.00
29.82444 0.00000000 1 1 22.00.00
29.97447 0.00000000 1 1 23.00.00
30.05386 0.00000000 1 2 00.00.00
30.17622 0.00000000 1 2 01.00.00
26.37835 0.00000000 1 2 02.00.00
25.70272 0.00000000 1 2 03.00.00
...

Réponse acceptée

Walter Roberson
Walter Roberson le 24 Déc 2017
fid = fopen('YourFile.txt', 'rt');
datacell = textscan(fid, '%s%*s%*s%*s%s', 'CollectOutput', true);
fclose(fid);
data_to_print = [{'ln Temp', 'Time'}; datacell{1}] .'; %transpose is important
fmt = '%8s%15s\n';
fid = fopen('NewFile.txt', 'wt');
fprintf(fid, fmt, data_to_print{:});
fclose(fid);
  1 commentaire
Bruno Souza
Bruno Souza le 24 Déc 2017
Thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 24 Déc 2017
Try this to have the user specify the input file and the output file and create the output file.
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd;
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select input file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullInputFileName = fullfile(folder, baseFileName)
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd;
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Select output file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullOutputFileName = fullfile(folder, baseFileName)
% Open the files.
inputFileID = fopen(fullInputFileName, 'rt');
outputFileID = fopen(fullOutputFileName, 'wt');
% Write hearder line to output file.
fprintf(outputFileID, 'In Temp Time\n');
% Read the first line of the file.
textLine = fgetl(inputFileID);
while ischar(textLine)
% Read the remaining lines of the file.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(inputFileID);
if textLine == -1
% End of file so break
break;
end
% Delete columns 9-39
textLine(9:33) = [];
% Write the line out to the output file.
fprintf(outputFileID, '%s\n', textLine);
end
% All done reading all lines, so close the files.
fclose(inputFileID);
fclose(outputFileID);
% Open it in Notepad or whatever the default program for opening a text file is
winopen(fullOutputFileName);
  1 commentaire
Bruno Souza
Bruno Souza le 24 Déc 2017
thank you

Connectez-vous pour commenter.

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by