importing .txt file using textscan

4 Ansichten (letzte 30 Tage)
douglas
douglas am 4 Apr. 2012
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)

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 4 Apr. 2012
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 Kommentare
douglas
douglas am 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 am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

douglas
douglas am 5 Apr. 2012
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.

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by