importing .txt file using textscan

Good Afternoon, I am trying to import data from a text file in order to plot, and need help writing a scipt.
the text file has text as well as numerical data within it so the load function wont work.
I don't fully understand how the textscan function works, my text data has the first 4 rows with just text, and then the columnheaders in each column, (row 5). Every row past this is just space separated numerical data.
How would I write the command out as to import the data into a usable format to plot with? The data is seprated by spaces (no commas)

 Risposta accettata

fid = fopen('YourFile.txt','rt');
datacell = textscan(fid, '%f%f%f', 'HeaderLines',5);
fclose(fid);
After that, datacell{1} is a vector containing the first column, datacell{2} is a vector containing the second column, datacell{3} is a vector containing the third column.

4 Commenti

douglas
douglas il 4 Apr 2012
Is there a way to just have it display the data as an mxn matrix with the data displayed as it was in the text file? where m is the number of rows and n is the number of columns?
fid = fopen('YourFile.txt','rt');
datacell = textscan(fid, '%f%f%f', 'HeaderLines', 5, 'CollectOutput');
fclose(fid);
After that, datacell{1} would be the m x 3 matrix.
douglas
douglas il 4 Apr 2012
I followed this and did indeed get my 3 columns (datacell{1,1} to datacell {1,3} etc) but the data is jumbled and random.
this is what I have so far:
clear all
close all
cd('C:\Documents and Settings\dgraham\Desktop\78 TEXT FORMAT')
fid = fopen('MT_00051-001.txt','rt');
datacell = textscan(fid, '%f%f%f','delimiter',' ','HeaderLines',5);
fclose(fid);
douglas
douglas il 4 Apr 2012
// Start Time: 0
// Sample rate: 10.0Hz
// Scenario: 3.8
// Firmware Version: 2.5.1
Year Month Day Second Counter Acc_X Acc_Y Acc_Z
0 0 0 0 0 -2.106693 -0.116872 9.583546
0 0 0 0 1 -2.104776 -0.058476 9.625679
0 0 0 0 2 -2.084688 -0.14616 9.588604
0 0 0 0 3 -2.053025 -0.097248 9.568006
0 0 0 0 4 -2.126364 -0.175665 9.635413
This is an example of one of the text files

Accedi per commentare.

Più risposte (1)

douglas
douglas il 5 Apr 2012

0 voti

figured out why my results were jumbled. If I have more columns than I specify in the format, it takes additonal data and puts it in the 3 columns it spits out. Thanks for the help.

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by