finding min and max values from a file
이전 댓글 표시
In a text file, I have a pressure listed. I am combining several files and I would like my program to find the highest pressure and lowest pressure. I think I use line=getl, but do not know how to format the line correctly, can you help me out?
Thank you!
답변 (2개)
Fangjun Jiang
2011년 6월 17일
0 개 추천
Please provide an example of your text file so we can help you. The task sounds not that hard. There are plenty of similar QAs regarding import text file for data. Once you get all the data, use min() and max() will get you the result.
Fangjun Jiang
2011년 6월 18일
Thank you for showing the example. I saw you posted similar questions a few times. Not sure how representative are these text files to your real data files. Your problem is trivial. The data format in your text file is irregular. You need to read the file and do specific string processing to get the data you want. I wrote the function below just show it can be done. But I suspect that it won't apply to your real data files. I hope you can use it as a starting point to get your task done.
Copy the following line to create ParseText.m file thus create the ParseText() function.
function [PR,FX,FY]=ParseText(TextFile)
fid=fopen(TextFile,'rt');
while ~feof(fid)
TextLine=fgetl(fid);
if strfind(TextLine,'Pressure:')
PR=textscan(TextLine,'%s%f%s');
PR=PR{2};
continue;
end
if strfind(TextLine,'FX:')
TextLine=strrep(TextLine,'FX:','');
FX=textscan(TextLine,'%f','delimiter',',');
FX=FX{1};
continue;
end
if strfind(TextLine,'FY:')
TextLine=strrep(TextLine,'FY:','');
FY=textscan(TextLine,'%s','delimiter',',');
FY=FY{1};
continue;
end
disp('Un-recognized text line.');
end
fclose(fid);
Then run the following lines, assume you have the three text files as shown in your comment.
>> [PR.PR1,FX.FX1,FY.FY1]=ParseText('File_20psi.txt');
>> [PR.PR2,FX.FX2,FY.FY2]=ParseText('File_30psi.txt');
>> [PR.PR3,FX.FX3,FY.FY3]=ParseText('File_40psi.txt');
>> Pressure=[PR.PR1,PR.PR2,PR.PR3]
Pressure =
20 30 40
>> FX_ALL=[FX.FX1;FX.FX2;FX.FX3]'
FX_ALL =
1 2 3 4 5 6 7 8 9
>> FY_ALL=[FY.FY1;FY.FY2;FY.FY3]'
FY_ALL =
'a' 'b' 'c' 'd' 'e' 'f'
>> max(Pressure)
ans =
40
>> min(Pressure)
ans =
20
댓글 수: 7
Melissa Patterson
2011년 6월 20일
Fangjun Jiang
2011년 6월 20일
The {} is used to reference the Matlab cell array data. Cell array is a flexible way to put different kind of data together. For example, NewCell={'abc', 1,1:3}, try NewCell{1},NewCell{2} and NewCell{3} at the Matlab Command window to see it yourself.
Fangjun Jiang
2011년 6월 20일
The reason to use PR=PR{2} in the code is because the prior line PR=textscan(TextLine,'%s%f%s') returns PR as a cell array. PR{2} is the pressure data that you want. You can put a break point in the code and then run the code line by line to see the results of every step.
Melissa Patterson
2011년 6월 20일
Melissa Patterson
2011년 6월 20일
Walter Roberson
2011년 6월 20일
For example, use
[R20,X20,Y20] = ParseText('File_20psi.txt');
Fangjun Jiang
2011년 6월 20일
Like Walter suggested, the code is for a M-function. You need to call it with an input argument 'File_20psi.txt', which is the name of the text file. The variable TextFile in the code will then has 'File_20psi.txt' as its value.
카테고리
도움말 센터 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!