importing .txt file using textscan

4 次查看(过去 30 天)
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)

采纳的回答

Walter Roberson
Walter Roberson 2012-4-4
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 个评论
douglas
douglas 2012-4-4
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 2012-4-4
// 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

请先登录,再进行评论。

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by