importing .txt file using textscan

조회 수: 3 (최근 30일)
douglas
douglas 2012년 4월 4일
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