MATLAB: Importing .txt file using textscan

data importMATLABtext filetextscan

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)

Best Answer

  • 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.