Skipping lines in text file

Hello, I have a text file called Acc that has two columns of data. The first has text and i want it to skipped. Can anyone help me solve it. I am a new user.
Here is how I load the data;
load 'Acc.txt';
time = Acc(:,1);
Acc_x = Acc(:,2);
Thank you
Muhsin

 Accepted Answer

Most likely, you could read the file very simply with:
t = readtable('Acc.txt');
which, if the first line is a header, should parse the header correctly and name the columns correctly. if that doesn't work you can always tell readtable to skip the first line:
t = readtable('Acc.txt', 'HeaderLines', 1);
Note that reading a file as a table is much better than popping variables with unpredictable names in your workspace.

5 Comments

Muhsin
Muhsin on 12 Oct 2017
Edited: Muhsin on 12 Oct 2017
Hello Guillaume;
can you please help me with attached file? It does not read the file as two columns even though it worked well just one hour ago. After I closed and reopened the software, the code stopped working. Here is my script;
clc;
clear;
t = readtable('Acc.txt');
time = Acc(:,1); %assigns the first column to be time (sec)
Acc_x = Acc(:,2); % assigns the second column to be acceleration (g)
Fs = 1/(time(2)-time(1)); %sampling frequency
L=length(Acc_x); %length of signal
T=1/Fs; %sampling period
Thank you
I gave you a way to correctly read your file using the dlmread function in my Answer to your original Question: single sided amplitude fourier spectrum (link).
Well, of course your doesn't work. The file is read into a table named t. It does not create a variable acc.
Please read about tables and how to use them. An easy way to do what you want:
acc = readtable('Acc.txt'); %read file into table acc
acc.Properties.VariableNames = {'Time', 'Acc_x'}; %rename columns of the table
FS = 1 / (acc.Time(2) - acc.Time(1));
L = height(acc);
T = 1 / FS;
Use acc.Time to get the time column and acc.Acc_x to get the acceleration column.
The code still gives me one column in workspace :(
No, the code gives you one table, acc, with two columns. As I said:
"Use acc.Time to get the time column and acc.Acc_x to get the acceleration column."
By the way, there's a lot of empty lines in your text file that matlab interpret as NaNs. To remove this invalid entries
acc = rmmissing(acc);
You can clearly see that acc is a table with two columns (variables):
>> summary(acc)
Variables:
Time: 1400×1 double
Values:
Min 0.01
Median 7.005
Max 14
Acc_x: 1400×1 double
Values:
Min -0.22004
Median -0.0058509
Max 0.20067

Sign in to comment.

More Answers (0)

Categories

Asked:

on 12 Oct 2017

Commented:

on 13 Oct 2017

Community Treasure Hunt

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

Start Hunting!