finding min and max values from a file
Show older comments
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!
Answers (2)
Fangjun Jiang
on 17 Jun 2011
0 votes
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.
1 Comment
Melissa Patterson
on 17 Jun 2011
Fangjun Jiang
on 18 Jun 2011
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 Comments
Melissa Patterson
on 20 Jun 2011
Fangjun Jiang
on 20 Jun 2011
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
on 20 Jun 2011
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
on 20 Jun 2011
Melissa Patterson
on 20 Jun 2011
Walter Roberson
on 20 Jun 2011
For example, use
[R20,X20,Y20] = ParseText('File_20psi.txt');
Fangjun Jiang
on 20 Jun 2011
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.
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!