Code covered by the BSD License  

Highlights from
operationOnTwoFields

image thumbnail

operationOnTwoFields

by

 

17 May 2012 (Updated )

A simple mathematical operation like subtracting on two 2d fields with multiple scalar values.

fieldDataComparison(operator, interpolationMethod, numOfVelocityComponents)
function fieldDataComparison(operator, interpolationMethod, numOfVelocityComponents)
%Simple helper function to use operationOnTwoFields() efficiently:
%
%fieldDataComparison(operator, interpolationMethod, numOfVelocityComponents)
%
%Upon call, it will ask for two set of files which must contain the same
%number of files. It will then operate on the first file of set one and the
%first file of set two etc:
%
%nth_file_setOne operator nth_file_setTwo
%
%and write the result in a subfolder of your current directory.
%
%operator can be one of: '+'; '-'; '.*'; './'
%
%setTwo is interpolated using TriScatteredInterp to match setOne.
%
%interpolationMethod used by TriScatteredInterp:
%
%       'natural'   Natural neighbor interpolation
%       'linear'    Linear interpolation
%       'nearest'   Nearest neighbor interpolation
%
%from '$help TriScatteredInterp'
%
%AThis is merely an example of how to (maybe) use
%operationOnTwoFields and is not supposed to be a general purpose function.
%In fact, it's use is very limited, it only works on files with one
%headerline, which are seperated by ';' and in the following format:
%
%x ; y ; z ; u ; v ; w ; magnitude(u,v,w)
%
%since I needed to compare velocity vector fields. You need to supply the
%number of velocity components (2 or 3) used to calculate the vector
%magnitude. Most of the times 3 will work even when w = 0. However, if you
%use the './' operator and set two contains w = 0 the vector magnitude will
%be Inf or NaN. In that case you need to set numOfVelocityComponents
%to 2.
%
%
%by Bastian Tietjen, released March 2013 under a Simplified BSD licence.

%ask for the files
[nameFileOne, pathFileOne] = uigetfile('*.csv', 'Please select one or more data files', 'MultiSelect', 'on');
[nameFileTwo, pathFileTwo] = uigetfile('*.csv', 'Please select one or more data files', 'MultiSelect', 'on');

%if only one file was selected, nameFileOne is a string instead of a cell
%array
if iscell(nameFileOne) == 0
    nameFileOneTemp{1} = nameFileOne;
    clear nameFileOne
    nameFileOne = nameFileOneTemp;
    clear nameFileOneTemp
end

if iscell(nameFileTwo) == 0
    nameFileTwoTemp{1} = nameFileTwo;
    clear nameFileTwo
    nameFileTwo = nameFileTwoTemp;
    clear nameFileTwoTemp
end

%check if the same number of files was selected
if length(nameFileOne) == length(nameFileTwo)
    %do nothing
else
    error('fieldDataComparison:numberOfSelectedFilesNotEqual', ...
        'You have selected a different number of files');
end

%check if there is a foldername available or warn the user that he has to
%remove some of the old data
for n = 0:999
    foldername = ['fieldDataComparison', num2str(n, '%0.3i')];
    
    if (exist(foldername, 'dir') == 0)
        %the foldername is fine, nothing will be overwritten
        break
        
    elseif (n == 999)
        display(['I will only increase the index of the foldername to 999. ',...
            'That index is reached now. Please rename, delete or move ',...
            'your previous results. I will not continue.'])
        return
    end
end



%use a for loop to call operationOnTwoFields() for each data set
for n = 1:length(nameFileOne)
    %Tell the user what is currently being done
    display([10, 'Working on '])
    display(nameFileOne{n})
    display(nameFileTwo{n})
    
    
    %load the data
    fileOneTemp = importdata([pathFileOne, nameFileOne{n}], ';', 1);
    fileTwoTemp = importdata([pathFileTwo, nameFileTwo{n}], ';', 1);
    
    %extract the data (no headerlines)
    fileOne = fileOneTemp.data;
    fileTwo = fileTwoTemp.data;
    
    clear fileOneTemp fileTwoTemp;
    
    resultingField(:,[1 2 4 5 6]) = operationOnTwoFields(fileOne(:,[1 2 4 5 6]), fileTwo(:,[1 2 4 5 6]), operator, interpolationMethod);
    %copy z-Position
    resultingField(:,3) = fileOne(1,3);
    
    %calculate the velocity magnitude
    resultingField(:,7) = zeros();
    for i = 4:3+numOfVelocityComponents
        resultingField(:,7) = resultingField(:,7) + resultingField(:,i).^2;
    end
    resultingField(:,7) = sqrt(resultingField(:,7));
    
    %save
    if 1 == n
        mkdir(foldername);
    end
    savename = ['fieldDataComparison', num2str(n, '%0.3i'),'.csv'];
    header='x;y;z;u;v;w;mag';
    dlmwrite([foldername, '/',savename], header, '');
    dlmwrite([foldername, '/',savename], resultingField, 'delimiter', ';', 'precision', 9, '-append');
    display([10, 'Saved in ', foldername, ' as ', savename, 10]);
    
end

%finally write in a simple text file in that folder what has been done on
%which files
fid = fopen([foldername, '/summary.txt'], 'a');

fprintf(fid, '%s%s%s%s%s\n\n', 'The following files were compared using the ',...
    operator, ' operator and a ', interpolationMethod, ' interpolation:');

fprintf(fid, '%s\n\n', 'Set one:');

fprintf(fid, '%s\n\n', pathFileOne);

for n = 1:length(nameFileOne)
    fprintf(fid, '%s\n', nameFileOne{n});
end

fprintf(fid, '\n%s\n\n', 'Set two:');

fprintf(fid, '%s\n\n', pathFileTwo);

for n = 1:length(nameFileTwo)
    fprintf(fid, '%s\n', nameFileTwo{n});
end

fclose(fid);

end

Contact us