how to load a txt data
2 次查看(过去 30 天)
显示 更早的评论
I cannot load following txt data into matlab, I have tried many functions,
BEGIN IONS
TITLE=445.1185_908.046
PEPMASS=445.1185
RTINSECONDS=908.046
CHARGE=1
355.074005126953 543
223.07470703125 59
150.026992797852 97
END IONS
I would like sort then this data as
TITLE PEPMASS RTINSECONDS CHARGE
445.118 445.118 908.046 1 355.0740 223.0747 150.0269
1 个评论
Image Analyst
2013-2-3
编辑:Image Analyst
2013-2-3
You didn't say what you want. For example, is Title a string? Or two floating point numbers separated by an underline? Is CHARGE = 1 or does it = [1, 355.074005126953, 543, 223.07470703125, 59, 150.026992797852, 97]? Or do you simply want a cell array of strings where each string is one line of the file? No one can give a complete answer unless they know things like that.
回答(3 个)
Azzi Abdelmalek
2013-2-3
编辑:Azzi Abdelmalek
2013-2-3
fid = fopen('file.txt');
line1 = fgetl(fid);
res=line1;
while ischar(line1)
line1 = fgetl(fid);
res =char(res,line1);
end
fclose(fid);
display(res)
0 个评论
Miroslav Balda
2013-2-4
Your code doesn't create or print data in the required format. The following code does it. The input data are in file IONS.txt:
% IONS.txt = comment
BEGIN IONS
TITLE=445.1185_908.046
PEPMASS=445.1185
RTINSECONDS=908.046
CHARGE=1
355.074005126953 543
223.07470703125 59
150.026992797852 97
END IONS
The following program creates the output that you required:
% IONSrw.m
%%%%%%%%%%%%%% 4.2.2013
clc
clear
% Function ffread.m is available from File Exchange
% www.mathworks.com/matlabcentral/fileexchange/9034
ffread('IONS.txt', '='); % file name
ffread(2,''); % skip 1st line
TI = ffread(2,'');
PE = ffread(2,'');
RT = ffread(2,'');
CH = ffread(2,'');
d = ffread(6,'');
ffread(2,''); % skip lastline
%
w = TI{2};
n = findstr(w,'_');
fprintf('%s %s %s %s\n', TI{1}, PE{1}, RT{1}, CH{1});
fprintf('%s %s %s %s %s %s %s\n', ...
w(1:n-1), PE{2}, RT{2}, CH{2}, d{1}, d{3}, d{5});
THe program uses function ffread.m for free format reading of text files of the known structure.
2 个评论
Miroslav Balda
2013-2-4
The result is
TITLE PEPMASS RTINSECONDS CHARGE
445.1185 445.1185 908.046 1 355.074005126953 223.07470703125 150.026992797852
Only the last number is in the same line as the other numbers.
Miroslav Balda
2013-2-5
You have to ask properly, otherwise you get an answer not fulfilling your wishes.
Miroslav Balda
2013-2-5
The general program for processing files of presented structure follows:
% IONS.m Answers: Mohamad, 3.2.2013, 12:14, Tags: load txt data
%%%%%%%%%%%%%% 4.2.2013
% Reads txt files using ffread.m for free format input
clc
clear
% Function ffread.m is available from File Exchange
% www.mathworks.com/matlabcentral/fileexchange/9034
% Function inp.m is available from File Exchange
% www.mathworks.com/matlabcentral/fileexchange/9033
file = inp('File name','IONS.txt','%s',5);
fprintf(' Processing of the file %s\n\n', file);
ffread(file, '='); % file name
while 1 % CYCLE OF LINES
w = ffread(1,''); % skip 'BEGIN'
if w==127, break,end % the end of data
ffread(1,''); % skip 'IONS'
TI = ffread(2,'');
n = findstr(TI{2},'_'); % position of char '_'
PE = ffread(2,'');
RT = ffread(2,'');
CH = ffread(2,'');
fprintf('%s %s %s %s\n', TI{1}, PE{1}, RT{1}, CH{1});
w = RT{2};
w = [w repmat(' ',1,7-length(w))];
fprintf('%s %s %s %s ', TI{2}(1:n-1), PE{2}, w, CH{2})
while 1
d = ffread(2,''); % read one line
if double(d{1}(1))<58 % digit character
fprintf(' %s', d{1});
else
fprintf('\n'); % skip line with 'END IONS'
break
end
end
end
The processing of the file from the Comment 3 yielded the following output:
File name = IONS.txt => IONS2.txt
Processing of the file IONS2.txt
TITLE PEPMASS RTINSECONDS CHARGE 445.1185 445.1185 908.046 1 355.074005126953 223.07470703125 150.026992797852 TITLE PEPMASS RTINSECONDS CHARGE 610.1826 610.1826 -1 1 TITLE PEPMASS RTINSECONDS CHARGE 536.1563 536.1563 -1 1 TITLE PEPMASS RTINSECONDS CHARGE 519.1368 519.1368 -1 1 TITLE PEPMASS RTINSECONDS CHARGE 446.1154 446.1154 -1 1 TITLE PEPMASS RTINSECONDS CHARGE 355.0693 355.0693 933.396 1 281.057800292969 269.013793945313 TITLE PEPMASS RTINSECONDS CHARGE 429.0904 429.0904 939.756 1 361.027008056641 372.740997314453 148.073593139648 324.981506347656 327.983703613281 TITLE PEPMASS RTINSECONDS CHARGE 536.1701 536.1701 -1 1 TITLE PEPMASS RTINSECONDS CHARGE 519.1362 519.1362 -1 1 TITLE PEPMASS RTINSECONDS CHARGE 446.1246 446.1246 956.694 1 283.046813964844 416.035797119141 147.065902709961 356.092590332031 400.990814208984 TITLE PEPMASS RTINSECONDS CHARGE 372.1053 372.1053 959.136 1 283.046813964844 147.065902709961 356.092590332031 343.017700195313 325.000305175781 294.950592041016 299.058197021484 223.085906982422 359.019104003906 326.973602294922 223.974197387695 268.014495849609 TITLE PEPMASS RTINSECONDS CHARGE 504.1139 504.1139 -1 1 TITLE PEPMASS RTINSECONDS CHARGE 391.2884 391.2884 -1 1
It is necessary to mention that the lines with many long numbers are here formated in such a way that lines are broken on new line(s).
5 个评论
Miroslav Balda
2013-2-7
The output of the program IONS.m should be on the screen provided your data are stored in the file named IONS2.txt stored in the same directory as the program IONS. You can't find results somewhere else. If you like to put them into a file, you need to modify all fprintf statements in IONS.m into fprintf(fid,...), where fid has been assigned by you calling fid=fopen('name of output file','w'), and dots correspond to the original text in the command. The program should be appended by the statement fclose(fid);
If the data file IONS2.txt does not exist in the current directory, a warning should appear on the screen. If the output from your last Comment be true, your file IONS2.txt were empty. It had the following form in my tests:
% IONS2.txt
%%%%%%%%%%% 5.2.2013
BEGIN IONS
TITLE=445.1185_908.046
PEPMASS=445.1185
RTINSECONDS=908.046
CHARGE=1
355.074005126953 543
:
: etc
:
BEGIN IONS
TITLE=391.2884_-1
PEPMASS=391.2884
RTINSECONDS=-1
CHARGE=1
END IONS
Miroslav Balda
2013-2-10
You have to name your data by any name (or use the names the files already have). Then run the file IONS.m and instead of the name IONS.txt offered by the program, you enter from a keyboard the name of the file you wish to process.
Have you the data in the named file? Use the full name of the file as an answer to the offer
File name = IONS.txt => % here insert the full name of your data file
It is all. I am unable to help you more. If you don't get any result, ask somebody in the vicinity to help you to enter the name of your file. Good luck!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!