Importing and Presenting Data in MATLAB
VerifiedAdded on Ā 2022/11/10
|6
|1081
|215
AI Summary
This document explains how to import data from a text file and present it in MATLAB. It also covers sorting data, performing calculations, and using functions. The document includes sample code and functions to help you get started.
Contribute Materials
Your contribution can guide someoneās learning journey. Share your
documents today.
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.
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.
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
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
[\,]*)*[\.]{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)
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)
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
Functions
Function to multiply the vectors
function aa=multi(bb,a,b,c)
[x y]=size(bb);% Obtaining the number of rows and columns
for i=1:x% Multiplying the atoms vector according to vectors a, b and c
if i>x
break;
else
bb(i,1:3)=a.*bb(i,1:3);
end
if i+1>x
break;
else
bb(i+1,1:3)=b.*bb(i+1,1:3);
end
if i+2>x
break;
else
bb(i+2,1:3)=c.*bb(i+2,1:3);
end
i=i+2;
end
aa=bb;
end
Function to multiply the vectors
function aa=multi(bb,a,b,c)
[x y]=size(bb);% Obtaining the number of rows and columns
for i=1:x% Multiplying the atoms vector according to vectors a, b and c
if i>x
break;
else
bb(i,1:3)=a.*bb(i,1:3);
end
if i+1>x
break;
else
bb(i+1,1:3)=b.*bb(i+1,1:3);
end
if i+2>x
break;
else
bb(i+2,1:3)=c.*bb(i+2,1:3);
end
i=i+2;
end
aa=bb;
end
% Function to plot the data
function bb=plott(cc)
figure;
plot3(cc(:,1),cc(:,2),cc(:,3),'o')
grid on;
end
Published with MATLABĀ® R2018b
function bb=plott(cc)
figure;
plot3(cc(:,1),cc(:,2),cc(:,3),'o')
grid on;
end
Published with MATLABĀ® R2018b
1 out of 6
Your All-in-One AI-Powered Toolkit for Academic Success.
Ā +13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Ā© 2024 Ā | Ā Zucol Services PVT LTD Ā | Ā All rights reserved.