logo

Importing and Presenting Data in MATLAB

   

Added on  2022-11-10

6 Pages1081 Words215 Views
Clearing and closing all MATLAB windows..........................................................1
Import data from text file................................................................................... 1
Sorting data........................................................................................................ 2
Calculations........................................................................................................ 2
Presenting data.................................................................................................. 2
Functions............................................................................................................ 4
Clearing and closing all MATLAB windows
clear;
clc;
close all;
Import data from text file.
Script for importing data from the following text file: To extend the code to
different selected data or a different text file, generate a function instead of a
script. Initialize variables.
filename = 'C:\Users\User\Desktop\NERDY\1076792\22061663_CONTCAR_2.txt';% Change the folder name to
the one you have stored your text file.
delimiter = ' ';
% Read columns of data as text:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%s%s%[^\n\r]';
% Open the text file.
fileID = fopen(filename,'r');
% Read columns of data according to the format.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'TextType', 'string',
'ReturnOnError', false);
% Close the text file.
fclose(fileID);
% Convert the contents of columns containing numeric text to numbers.
% Replace non-numeric text with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = mat2cell(dataArray{col}, ones(length(dataArray{col}), 1));
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,2,3,4]
% Converts text in the input cell array to numbers. Replaced non-numeric
% text with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1)
% Create a regular expression to detect and remove non-numeric prefixes and

% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+
[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData(row), regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if numbers.contains(',')
thousandsRegExp = '^[-/+]*\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(numbers, thousandsRegExp, 'once'))
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric text to numbers.
if ~invalidThousandsSeparator
numbers = textscan(char(strrep(numbers, ',', '')), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch
raw{row, col} = rawData{row};
end
end
end
% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
% Create output variable
CONTCAR2 = cell2mat(raw);
% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result
numbers invalidThousandsSeparator thousandsRegExp R;
Sorting data
a=CONTCAR2(3,1:3); % Change the variables names according to your data in your text file
b=CONTCAR2(4,1:3); % Matrix containing the vectors a,b,c
c=CONTCAR2(5,1:3);
Na=CONTCAR2(9:15,1:3); % Matrix containing vectors of the various atoms e.g. Na
Mn=CONTCAR2(16:21,1:3);
O=CONTCAR2(22:37,1:3);
Li=CONTCAR2(38:39,1:3);
Calculations
Multiplying vectors a, b and c with atom vectors

Mn=multi(Mn,a,b,c);
Na=multi(Na,a,b,c);
O=multi(O,a,b,c);
Li=multi(Li,a,b,c);
% Calculating dimensions
Presenting data
plott(Na)
plott(Mn)
plott(Li)
plott(O)

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Hybrid Method Analysis using ANN, SVM and DT for Machine Learning
|6
|1141
|407