MATLAB Answers

Importing ascii file in matlab with tab as delimiter

8 views (last 30 days)
Sandeep Nair
Sandeep Nair on 30 Nov 2020
Answered: Ameer Hamza on 30 Nov 2020
I need to import the ascii file from matlab script with tab as delimiter and the variables names should be from row 3 and
the data to be imported from A6 to D1000 . Can anyone please help me on this

  3 Comments

Ameer Hamza
Ameer Hamza on 30 Nov 2020
Can you share a sample file, and what is your expected output? Also, note that naming variables dynamically is not a good idea: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. You should not store the data in the variable name. Use an array to store the data or use some other data structures such as table() or struct().
Sandeep Nair
Sandeep Nair on 30 Nov 2020
Please find the sample file, I want to take the variable from 3rd row ie time A B C and data from numbers
ETASAsciiItemFile record CrLf Tab
sampleCount 74368
time A B C
f8 u8 u8 u8
s - -
1 1 0 1
1 1 0 1
1 1 0 1
1 1 0 1
1 1 0 1
1 1 0 1

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 30 Nov 2020
You can try the following.
if true
fname = 'pathtofile.txt';
opts = delimitedTextImportOptions('VariableNamesLine',3,'DataLines',6,'Delimiter','\t');
out = readtable(fname,opts);
end

  0 Comments

Sign in to comment.

More Answers (1)

Ameer Hamza
Ameer Hamza on 30 Nov 2020
On R2020b, you can simply use readtable() on the file itself
T = readtable('data.txt')
'data.txt' is attached.
>> T.time
ans =
1
1
1
1
1
1
>> T.A
ans =
1
1
1
1
1
1
>> T.B
ans =
0
0
0
0
0
0
>> T.C
ans =
1
1
1
1
1
1

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!