The splitting text contains file into separate columns

6 vues (au cours des 30 derniers jours)
Furat Alobaidy
Furat Alobaidy le 18 Nov 2019
Commenté : Furat Alobaidy le 19 Nov 2019
hi i have text files contains strings and number , i need seperate the string from numbers into excel columns file :
as in attache file:
example :
system.mem_::UNDEFINED 1713974500 # Cumulative ticks
system.cpu05.kern.ipl_ticks::31 6939000 0.77% 100.00% # number of
so i need put them in seperate culumns as following :
Col A Col B Col C Col D Col E Col H
system.mem_::UNDEFINED 1713974500 #Cumulative ticks
system.cpu05.kern.ipl_ticks:: 31 6939000 0.77% 100.00% # Number of
i used import file by matlab : but it seperate only into two cloums such as this :
A B
system.mem_::UNDEFINED 1713974500 # Cumulative ticks
I appriciate for any help !

Réponse acceptée

Akira Agata
Akira Agata le 19 Nov 2019
How about the following?
% Read txt file
fid = fopen('data1.txt','r');
strData = textscan(fid,'%s','Delimiter','\r\n');
strData = strData{1};
fclose(fid);
% Eliminate empty line(s) and the line(s) which contains '--'
idx1 = cellfun(@isempty,strData);
idx2 = contains(strData,'--');
strData = strData(~idx1 & ~idx2);
% Insert space before '#'
strData = replace(strData,'#',' #');
% Replace 3 or more consecutive spaces to 2 consecutive spaces
strData = regexprep(strData,'\s{3,}',' ');
% Prepare the output cell array
outData = cell(size(strData,1),6);
% Split each line with 2 consecutive spaces
for kk = 1:size(strData,1)
c = strsplit(strData{kk},' ');
outData(kk,[1:numel(c)-1, end]) = c;
end
% Save as Excel file
writecell(outData,'result.xlsx');
  1 commentaire
Furat Alobaidy
Furat Alobaidy le 19 Nov 2019
yes it's professional work!, thanks a lots.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by