Azzera filtri
Azzera filtri

Import data from text file

28 visualizzazioni (ultimi 30 giorni)
Ali Agha
Ali Agha il 14 Gen 2022
Commentato: Kevin Holly il 18 Gen 2022
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.
  5 Commenti
Ali Agha
Ali Agha il 14 Gen 2022
I have this sample of the data
Ive J
Ive J il 14 Gen 2022
Modificato: Ive J il 14 Gen 2022
You need to mung your txt file, first few lines are totally messed up. Maybe you manually modified it (?).
doc readtable
doc readmatrix
doc readlines % to get a feel on your data structure

Accedi per commentare.

Risposta accettata

Kevin Holly
Kevin Holly il 14 Gen 2022
Modificato: Kevin Holly il 14 Gen 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)
ans = 1×2
90112 2
data=DataChan1(2:end,1:2);
size(data)
ans = 1×2
90111 2
data
data = 90111×2 table
Time Amplitude __________ __________ 0.00019531 2.8167e-05 0.00039063 2.8191e-05 0.00058594 2.8337e-05 0.00078125 2.8255e-05 0.00097656 2.7935e-05 0.0011719 2.7522e-05 0.0013672 2.7267e-05 0.0015625 2.7049e-05 0.0017578 2.6771e-05 0.0019531 2.6771e-05 0.0021484 2.6546e-05 0.0023437 2.6159e-05 0.0025391 2.6179e-05 0.0027344 2.5608e-05 0.0029297 2.4605e-05 0.003125 2.3894e-05
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 Commenti
Ali Agha
Ali Agha il 14 Gen 2022
Thank you Kevin, it's really helpful to solve the issue but I'm still facing problem in running the data into the script below: appreciate if you can check it. For many days not able to identify the problem.
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
path = 'rub-impact/group1/900rpm/Throughput/Data_Chan1.txt');
time=DataChan1(:,1); % time sequency
x=DataChan1(:,2); % displacement signal sequency (x, y, or keyphasor, here is x)
fs=10240; % samling freuency
% plot time waveform
figure
plot(t,x)
% plot spectrum
figure
plotfft(x,fs,'b')
% modify the path to the file to be analysed
path_2=load('rub-impact/group1/900rpm/Throughput/Data_Chan2.txt');
data=path(2:end,1:2);
t=data(:,1); % time sequency
y=data(:,2); % displacement signal sequency (x, y, or keyphasor, here is y)
% plot time waveform
figure
plot(t,y)
% plot spectrum
figure
plotfft(y,fs,'b')
% modify the path to the file to be analysed
path_3=load('rub-impact/group1/900rpm/Throughput/Data_Chan3.txt');
data=path_3(2:end,1:2);
t=data(:,1); % time sequency
y=data(:,2); % displacement signal sequency (x, y, or keyphasor, here is y)
% plot time waveform
figure
plot(t,y)
% plot spectrum
figure
plotfft(y,fs,'b')
% modify the path to the file to be analysed
path_4=load('rub-impact/group1/900rpm/Throughput/Data_Chan4.txt');
data=path_4(2:end,1:2);
t=data(:,1); % time sequency
z=data(:,2); % displacement signal sequency (x, y, z or keyphasor, here is z)
% plot time waveform
figure
plot(t,z)
% plot spectrum
figure
plotfft(z,fs,'b')
% plot keyphasor signal
path=load('rub-impact/group1/900rpm/Throughput/Data_Chan1.txt');
keyphasor=path(:,2); % displacement signal sequency (x, y, or keyphasor, here is keyphasor)
figure
plot(t,keyphasor)
% plot the orbit
path=load('rub-impact/group1/900rpm/Throughput/Data_Chan2.txt');
y=data(:,2); % displacement signal sequency (x, y, or keyphasor, here is y)
figure
plot(x,y)
Kevin Holly
Kevin Holly il 18 Gen 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)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Text Data Preparation in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by