importing .txt file using textscan

5 visualizaciones (últimos 30 días)
douglas
douglas el 4 de Abr. de 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)

Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Abr. de 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 comentarios
douglas
douglas el 4 de Abr. de 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 el 4 de Abr. de 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

Iniciar sesión para comentar.

Más respuestas (1)

douglas
douglas el 5 de Abr. de 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.

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by