Import data from text file
10 views (last 30 days)
Show older comments
I have a text files with two colums, when I used the
importfile(Data_Chan1.txt);
data=Data_Chan1(2:end,1:2);
I get error in reading the data.
Accepted Answer
Kevin Holly
on 14 Jan 2022
Edited: Kevin Holly
on 14 Jan 2022
I used the import data button on the toolstrip to import the text file. Then I generated the function below.
DataChan1 = importDataChanfile('Data_Chan1.txt');
size(DataChan1)
data=DataChan1(2:end,1:2);
size(data)
data
plot(data.Time,data.Amplitude)
function DataChan1 = importDataChanfile(filename, dataLines)
%IMPORTFILE Import data from a text file
% DATACHAN1 = IMPORTFILE(FILENAME) reads data from text file FILENAME
% for the default selection. Returns the data as a table.
%
% DATACHAN1 = IMPORTFILE(FILE, DATALINES) reads data for the specified
% row interval(s) of text file FILENAME. Specify DATALINES as a
% positive scalar integer or a N-by-2 array of positive scalar integers
% for dis-contiguous row intervals.
%
% Example:
% DataChan1 = importfile("C:\Users\kevinh\OneDrive - MathWorks\Desktop\Data_Chan1.txt", [19, Inf]);
%
% See also READTABLE.
%
% Auto-generated by MATLAB on 14-Jan-2022 13:29:17
%% Input handling
% If dataLines is not specified, define defaults
if nargin < 2
dataLines = [19, Inf];
end
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 2);
% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["Time", "Amplitude"];
opts.VariableTypes = ["double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Import the data
DataChan1 = readtable(filename, opts);
end
2 Comments
Kevin Holly
on 18 Jan 2022
close all;clear;clc;
% Data_Chan1.txt --- x-dirention displacement signal
% Data_Chan2.txt --- y-dirention displacement signal
% Data_Chan3.txt --- y-acceleration signal
% Data_Chan4.txt --- z-acceleration signal
% modify the path to the file to be analysed
DataChan1 = importDataChanfile('Data_Chan1.txt');
% Alternatively, you can the command below to select the file
% filename = uigetfile;
% DataChan1 = filename
% path = 'rub-impact/group1/900rpm/Throughput/Data_Chan1.txt');
time=table2array(DataChan1(:,1)); % time sequency
x=table2array(DataChan1(:,2)); % displacement signal sequency (x, y, or keyphasor, here is x)
fs=10240; % sampling freuency
% plot time waveform
figure
plot(time,x) %change t to time
xlabel('Time')
ylabel('Displacement Signal Sequency')
% plot keyphasor signal
keyphasor=table2array(DataChan1(:,2)); % displacement signal sequency (x, y, or keyphasor, here is keyphasor)
figure
plot(time,keyphasor)
% plot spectrum
figure
pspectrum(x,fs) % plotfft(x,fs,'b')
% Import DataChan2
DataChan2 = importDataChanfile('Data_Chan2.txt');
%
% % modify the path to the file to be analysed
% path_2=load('rub-impact/group1/900rpm/Throughput/Data_Chan2.txt');
data=DataChan2(2:end,1:2);
t=table2array(data(:,1)); % time sequency
y=table2array(data(:,2)); % displacement signal sequency (x, y, or keyphasor, here is y)
% plot time waveform
figure
plot(t,y)
% plot spectrum
figure
pspectrum(y,fs)
% plot the orbit
figure
plot(x,y(1:length(x)))
% Import DataChan3
DataChan3 = importDataChanfile('Data_Chan3.txt');
data=DataChan3(2:end,1:2);
t=table2array(data(:,1)); % time sequency
y=table2array(data(:,2)); % displacement signal sequency (x, y, or keyphasor, here is y)
% plot time waveform
figure
plot(t,y)
% plot spectrum
figure
pspectrum(y,fs)
% Import DataChan4
DataChan4 = importDataChanfile('Data_Chan4.txt');
data=DataChan4(2:end,1:2);
t=table2array(data(:,1)); % time sequency
z=table2array(data(:,2)); % displacement signal sequency (x, y, or keyphasor, here is y)
% plot time waveform
figure
plot(t,z)
% plot spectrum
figure
pspectrum(z,fs)
More Answers (0)
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!