Extract data from a file, but without knowing the data's specific location within the file?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello MATLAB Answers,
I am having troubles with a particular problem. The problem asks me to extract certain data from a file and then with that data either plot it or display. The problem is, is that I am not allowed to inspect the data to find where in the text file it is. Instead I am just giving the headers and what value names I am looking for. The names are MACH, ALPHA, CL, CD, x, lower y, higher y, lower cp, higher cp.
I have attached the text file with which I am supposed to extract data from. I have already written all of the plotting and displaying. I just need to get the specific values.
Here is the code I already have with some of the variables included.
% Extract data here% Extract data here
fid = fopen('N0012.out');
x = cell2mat(textscan(fid,
lowy = cell2mat(textscan(fid,
highy = cell2mat(textscan(fid,
cplow = cell2mat(textscan(fid,
cphigh = cell2mat(textscan(fid,
mach = cell2mat(textscan(fid,
alpha = cell2mat(textscan(fid,
cl = cell2mat(textscan(fid,
cd = cell2mat(textscan(fid,
fclose(fid);
% Plotting
figure;
plot(x, lowy);
hold on
plot(x, highy);
hold off
title('Airfoil NACA 0012');
xlabel('X coordinates of Airfoil');
ylabel('Y coordinates of Airfoil');
legend('Lower Y coordinates', 'Upper Y coordinates');
figure;
plot(x, cplow);
hold on
plot(x, cphigh);
hold off
title('Pressure Coefficient Distributionof NACA 0012');
%
%Make sure to include MACH, ALPHA, CL, and CD in Title above
%
xlabel('X coordinates');
ylabel('Pressure Coefficient');
legend('Lower Airfoil Pressure Coefficients', 'Upper Airfoil Pressure Coefficients');
fid = fopen('N0012.out');
x = cell2mat(textscan(fid,
lowy = cell2mat(textscan(fid,
highy = cell2mat(textscan(fid,
cplow = cell2mat(textscan(fid,
cphigh = cell2mat(textscan(fid,
mach = cell2mat(textscan(fid,
alpha = cell2mat(textscan(fid,
cl = cell2mat(textscan(fid,
cd = cell2mat(textscan(fid,
fclose(fid);
% Plotting
figure;
plot(x, lowy);
hold on
plot(x, highy);
hold off
title('Airfoil NACA 0012');
xlabel('X coordinates of Airfoil');
ylabel('Y coordinates of Airfoil');
legend('Lower Y coordinates', 'Upper Y coordinates');
figure;
plot(x, cplow);
hold on
plot(x, cphigh);
hold off
title('Pressure Coefficient Distributionof NACA 0012');
%
%Make sure to include MACH, ALPHA, CL, and CD in Title above
%
xlabel('X coordinates');
ylabel('Pressure Coefficient');
legend('Lower Airfoil Pressure Coefficients', 'Upper Airfoil Pressure Coefficients');
1 comentario
per isakson
el 13 de Oct. de 2015
Cannot understand which data are the values of "lower y, higher y, lower cp, higher cp"
Respuestas (1)
TastyPastry
el 13 de Oct. de 2015
Editada: TastyPastry
el 13 de Oct. de 2015
Well this text file is honestly a nightmare to parse, but I'd do the following, assuming that the format is the same for different airfoils. By no means is this the fastest way to do something like this, but I'll try to help.
This process pretty much assumes the order in which you find the parameters is the same, but it'll go line by line. There are a lot of methods of going through a text file line by line, but for this I'd prefer to just loop through it using fgetl().
- I know that MACH appears first as EMACH in the file, so my first while loop will turn false when strfind() matches somewhere with EMACH. You should remove the line you just read in containing EMACH and either store it as a variable to process later, or process it now and extract the mach value.
- The next value is ALPHA, so I'm going to use a similar process as above.
- The next value is X and Y upper and Y lower. I'm going to look for a line containing UPPER SURFACE LOWER SURFACE. I'll now skip a line (since those are headers) and read in lines until I find a blank line indicating the end of the coordinates. As I read in the coordinates, I'll parse using whitespace into a cell array and use str2double() on the contents to store the first, second and fourth values which correspond to X, Ylow and Yhi.
- Now, I'm going to look for the line containing CLFACT. After I find it, I'll skip a line and look for CL and CM. Read in the lines and extract values.
- Finally, I'm going to look for a line containing I X CP M1. I'll skip 2 lines and read in the third and fifth values in each line until I hit another blank line. I'll convert these into a vector as well. Be careful though, if you see the word AIRFOIL you cannot process that line since it may not have 5 delimited values.
- After that step I've got my information so I can just exit the program.
Honestly, you might find it faster (and more fun) to create a mesh and do some CFD analysis on whatever airfoils though...
0 comentarios
Ver también
Categorías
Más información sobre Airfoil tools en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!