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!

Answers (2)

Fangjun Jiang
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.

1 Comment

File_20psi.txt:
Pressure: 20 psi
FX: 1,2,3
FY: a,b
File_30psi.txt
Pressure: 30 psi
FX: 4,5,6
FY: c,d
File_40psi.txt
Pressure: 40 psi
FX: 7,8,9
FY: e,f

Sign in to comment.

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

Thank you! You are right that I didn't post my complete files, but I was looking for an example that would show me the syntax and layout of the programming I would need. I am not a programmer and would like to learn, but everything is so incredibly foreign to me, I'm not sure I will grasp MATLAB anytime soon. Can you tell me why, in the pressure section of your program what the {2} means? I notice there is a {1} in the FX and FY sections.
Thank you so much for your time, I really appreciate the help!
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.
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.
This has been very helpful, thank you!
I am sorry, the following just emphasizes how little I know: I get the following error when I run the .m file I created with your program:
>> ParseText
??? Input argument "TextFile" is undefined.
Error in ==> ParseText at 2
fid=fopen(TextFile,'rt');
I pasted what you sent me, this is what it looks like:
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);
Do you see what I did wrong?
For example, use
[R20,X20,Y20] = ParseText('File_20psi.txt');
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.

Sign in to comment.

Categories

Products

Tags

Asked:

on 17 Jun 2011

Community Treasure Hunt

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

Start Hunting!