Combining text files

1 次查看(过去 30 天)
Melissa Patterson
Melissa Patterson 2011-6-16
[EDIT: 20110616 11:05 CDT - reformat - WDR]
[EDIT: 20110616 10:25 CDT - merge from duplicate question - WDR]
[EDIT: 20110616 09:43 CDT - remove confidential material, reformat - WDR]
My company uses MATLAB for everything. I have MATLAB generated files that I want to combine. An simplified example of the files is below:
File1.txt:
Pressure: 20 psi
FY: 1,2,3
FX: a,b
File2.txt:
Pressure: 25 psi
FY: 4,5,6
FX: c,d
File3.txt:
Pressure: 30 psi
FY: 7,8,9
FX: e,f
I want to combine the files so that the output looks something like this:
Minimum Pressure: 20 psi
Maximum Pressure: 30 psi
FY: 1,2,3,4,5,6,7,8,9
FX: a,b,c,d,e,f
I have so little experience programming, that I do not even know how to read in the files. Please take pity on my and tell me how to start. Start? Heck! I need all the help I can get! Tell me everything you think will be helpful.
Thank you!
Here is what I have so far, and I know, I have failed miserably:
[Program removed as it contained company-confidential material - WDR]
[Material from duplicate question]
I have multiple MATLAB created text files that I need to combine. A simplified sample file looks like this:
File_1_D100.txt:
Pressure: 20 psi
D Number: D100
FX: a,b,c
FY: 1,2,3,4
File_2_D100.txt:
Pressure: 30 psi
D Number: D100
FX: d,e,f
FY: 5,6,7,8
I would like the output to look like:
Min Pressure: 20 psi
Max Pressure: 30 psi
D Number: D100
FX: a,b,c,d,e,f
FY: 1,2,3,4,5,6,7,8
1.)I don't know how to read in multiple files, 2.)I don't know how to combine blocks of data, 3.)and I don't know how to make the final file recognize the max and min pressures. This is what I have so far:
function Mult_Pressures_Tire_File_Reader
%
%
% Program Development Date : June 15, 2011
%
% Program Developed By : Melissa Patterson
%
% Units of output and internal parameters
%
%******************************************************************%
% The first step in the process is to read in the SES tire files and sort
% data by coefficient type.
%
%
%
clear all;
close all;
warning off;
% Load the appropriate text files
[fname,pname]=uigetfile('*.tir','Please select all pressures');
full_fname=[pname, fname];
load(full_name)
fname=fname(1:size(fname,2)-4);
answer= questdlg('Please enter D number:');
% Open and Write Output File
id = fopen(output_filename, 'wt');
fprintf(id, '!\n');
fprintf(id, '! Date Created : June 14, 2011\n');
fprintf(id, '! Created By : Melissa Patterson\n');
fprintf(id, '!\n');
fprintf(id, '[Tire Model Specifications]\n');
fprintf(id, 'Tire_Model_Version : 6.5\n');
fprintf(id, 'Tire_File_Version : 3.45\n');
fprintf(id, ' Length_Units : ''mm''\n');
fprintf(id, ' Force_Units : ''newton''\n');
fprintf(id, ' Angle_Units : ''degrees''\n');
fprintf(id, ' Mass_Units : ''kg''\n');
fprintf(id, ' Time_units : ''second''\n');
fprintf(id, '!\n');
fprintf(id, '[Tire Details]\n');
fprintf(id, 'Tire_Construction : %6.4f'\n',D_Number);
fprintf(id, 'Tire_Manufacturer : Goodyear\n');
fprintf(id, 'Tire_Year : 2011\n');
fprintf(id, 'Tire_Application : Road Course\n');
fprintf(id, 'Tire_Position : All !All, LF, RF, LR, RR, LS, RS\n');
fprintf(id, 'Tire_Series : Sprint_Cup ! Sprint Cup, Nationwide, Truck\n');
fprintf(id, '!\n');
fprintf(id, '[Testing Details]\n');
fprintf(id, 'Minimum_Pressure : %6.4f ! psi\n');
fprintf(id, 'Maximum_Pressure : %6.4f ! psi\n');
fprintf(id, 'Pressure Increments : %6.4f ! psi\n');
fprintf(id, 'LONGVL = %7.4f $Measurement speed\n',(str2num(Test_Speed))*0.44704);
fprintf(id, 'TYRESIDE = ''RIGHT'' $Mounted side of tyre at vehicle/test bench\n');
fprintf(id, '$--------------dimensions\n');
fprintf(id, '[DIMENSION]\n');
fprintf(id, 'UNLOADED_RADIUS = %6.4f $Free tire radius\n',ULR);
fprintf(id, 'WIDTH = %6.4f $Nominal section width of the tyre\n',(str2num(Nom_Section_Width))/1000);
fprintf(id, 'ASPECT_RATIO = %6.4f $Nominal aspect ratio\n',Nom_Aspect_Ratio);
fprintf(id, 'RIM_RADIUS = %6.4f $Nominal rim radius\n',(str2num(Rim_Radius))*(25.4/2000));
fprintf(id, 'RIM_WIDTH = %6.4f $Rim width\n',(str2num(Rim_Width))*25.4/1000);
fprintf(id,
fclose all;
  2 个评论
Andrew Newell
Andrew Newell 2011-6-16
Could you please provide a more meaningful title for your question? See http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer. Also, some of the code needs formatting so we can read it easily - see http://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question.
Walter Roberson
Walter Roberson 2011-6-16
Somewhere along the line part of the program went missing -- the final fprintf() is an incomplete statement.

请先登录,再进行评论。

回答(1 个)

Baalzamon
Baalzamon 2011-7-7
How many files do you have? Are they all named and structured the same way?
First step:
Help >> Product help >> By category >> Data Import and Export

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by