finding min and max values from a file
3 views (last 30 days)
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!
0 Comments
Answers (2)
Fangjun Jiang
on 17 Jun 2011
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
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
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.
See Also
Categories
Find more on Data Import and Export 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!