Question: In matlab: Please help me combine this script that converts an airfoil dat file into a csv file, into this function that gets rid of

In matlab: Please help me combine this script that converts an airfoil dat file into a csv file, into this function that gets rid of the headerlines and other stuff within the dat file. The end goal should be the output folder containing the modified csv files. Thanks!

Script:

% Define the input and output folders

inputFolder = 'path/to/input/folder';

outputFolder = 'path/to/output/folder';

% Get a list of all .dat files in the input folder

datFiles = dir(fullfile(inputFolder, '*.dat'));

% Loop through the .dat files and convert them to .csv

for i = 1:length(datFiles)

% Read the data from the .dat file

data = importdata(fullfile(inputFolder, datFiles(i).name));

% Get the file name without the extension

[~, fileName, ~] = fileparts(datFiles(i).name);

% Write the data to a .csv file in the output folder

csvFile = fullfile(outputFolder, [fileName '.csv']);

csvwrite(csvFile, data);

end

function [finalM] = matrix_array(importfile,exportfile) % Will create an output file, but we will use csv

hdrlns = 1;

if (strcmp(importfile,'nasasc2-0714'))

hdrlns = 3;

elseif (strcmp(importfile,'s1020'))

hdrlns = 2;

end

Airfoil = fopen(importfile); % Opens the airfoil data file

dataBuffer = textscan(Airfoil,'%f %f','CollectOutput', 1,'HeaderLines', hdrlns ,'Delimiter',''); % Reads the airfoil data; %f represents floating point numbers; CollectOutput to 1 (true) to collect the consecutive columns of the same class into a single array; Set HeaderLines to 1

dataX = dataBuffer{1}(:,1); % Airfoil X-data

dataY = dataBuffer{1}(:,2); % Airfoil Y-Data

fclose(Airfoil); % Closes the airfoil data file

dataArr = [dataX dataY]; % Delete any duplicate (0,0) lines (only need one)

[~,ia,~] = unique(dataArr,'rows','stable'); % Find the unique values of the array

i = true(size(dataArr,1),1); % Set every index to true

i(ia) = false; % Set indices false that are not duplicates

dataArr(i,:) = []; % Get rid of duplicate rows (true to i)

dataX = dataArr(:,1); % Separate out X-data

dataY = dataArr(:,2); % Separate out Y-data

if (dataY(1) ~= dataY(end)) % If the start and end points are not the same

dataX(end+1) = dataX(1); % Create a new X-endpoint to close the airfoil

dataY(end+1) = dataY(1); % Create a new Y-endpoint to close the airfoil

end

finalM = [dataX dataY]; % Outputs variables

writematrix(finalM,exportfile) % User able to get export file that can be renamed

end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!