MATLAB Answers

Question about import a .txt file into matlab

13 views (last 30 days)
I'm trying to import a .txt data, but when i used the import data option, something goes wrong and does not appear the file that I want to analyse.
The file is attached. Thank you so much!


Bob Nbob
Bob Nbob on 10 Oct 2019
What exactly goes wrong? What command are you using to do the import?
Johan Sebastian Diaz Tovar
This is the code made by MATLAB itself and is not working, this is the error that im getting from it:
Error using textscan
Mismatch between file and format string.
Trouble reading 'Negative Character Set' field from file (row number
2, field number 1) ==>
Date: Tue Mar 19 16:18:18 BRT 2019\n
Error in test01 (line 28)
textscan(fileID, '%[^\n\r]', startRow-1, 'ReturnOnError', false);
I attached a better file to open, the other one is just a baseline, sorry.
Initialize variables.
filename = 'G:\Meu Drive\Mestrado\Experimento con ratones\espectroradiometro\2019-03-19\780 nm animal 1 x=4 y=4_AbsoluteIrradiance_16-18-18-480.txt';
startRow = 13;
%% Format string for each line of text:
% column1: text (%s)
% column2: text (%s)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%7s%9s%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
textscan(fileID, '%[^\n\r]', startRow-1, 'ReturnOnError', false);
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'EmptyValue' ,NaN,'ReturnOnError', false);
%% Remove white space around all cell columns.
dataArray{1} = strtrim(dataArray{1});
dataArray{2} = strtrim(dataArray{2});
%% Close the text file.
%% Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%% Allocate imported array to column variable names
Datafr = dataArray{:, 1};
om780nm = dataArray{:, 2};
%% Clear temporary variables
clearvars filename startRow formatSpec fileID dataArray ans;
Bob Nbob
Bob Nbob on 11 Oct 2019
I'm not sure about the exact error you're getting, but I would guess the error line is not reading because you have the row start line. In the textscan documentation the first three inputs are the file ID, the formatspec, and the number of repetitions you want to capture. The format spec you have specified for the error line returns a single cell which contains a series of strings for all individual lines of the file. For me, using the repetition number returned a blank cell with no data. I would suggest getting rid of that number (which I suspect is supposed to be the number of header lines).
Which bits of data are you trying to capture from the file? I see three integers, a comma, and two to three more integers, before a series of spaces with a 0 or -0 as the final characters. Are you looking to capture the numbers on either side of the comma? Or is that supposed to be one number, and the 0 is the second column?

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 11 Oct 2019
Edited: Stephen Cobeldick on 11 Oct 2019
Your code has several issues, the most significant ones are:
  1. you incorrectly used the optional third input, apparently to try and define the number of header lines. The correct way to specify the header lines is to use a key-value argument.
  2. your data uses decimal commas. MATLAB does not handle decimal commas, which means you need to convert the commas to points before converting to numeric: search this forum for different ways to do this.
Here is one simple solution using textscan's very convenient ability to parse character vectors:
fnm = '780 nm animal 1 x=4 y=4_AbsoluteIrradiance_16-18-18-480.txt';
str = fileread(fnm);
str = strrep(str,',','.');
opt = {'HeaderLines',13, 'CollectOutput',true};
fmt = '%f%f';
C = textscan(str,fmt,opt{:});
>> out = C{1}
out =
177.84 0
178.06 0
178.28 0
178.5 0
178.72 0
178.93 0
179.15 0
... lots of lines here
348.85 0
349.06 0
349.27 0
349.48 0
349.69 0
349.9 0
350.11 -0.46
350.32 -0.38
350.53 -0.41
350.74 -0.14
350.95 0.03
.... lots more lines here
890.36 -1.06
890.53 -1.71
890.7 -2.42
890.87 -1.44
891.04 -2.31
891.2 -2.19
891.37 -1.34
891.54 -1.7
891.71 -0.93
891.88 0.07
892.05 -0.88
892.22 -0.1
>> size(out)
ans =
3648 2


Show 2 older comments
Johan Sebastian Diaz Tovar
I used the exactly code you posted here and exactly the same file i uploaded earlier. And the cell is a 1x1 cell
I could upload another files if i would help.
Thank you so much!
Stephen Cobeldick
Stephen Cobeldick on 12 Oct 2019
I took a look at your files, and all of them have very mixed up newline characters:
You can see that:
  1. the first line ends with a linefeed character, but this is immediately followed by a carriage return character. This is extremely odd.
  2. the rest of the header uses linefeed characters (the Linux standard)...
  3. but then mysteriously the data uses carriage-return AND line feed characters (the Windows standard).
Very inconstent -> difficult to parse.
The behavior you described is consistent textscan quitting at the first carriage return on the first line of data, after automatically identifying the linefeed as the file's newline character (perhaps using the first line). Are you using Linux by any chance?
In any case, I would suggest two possible solutions to this:
  1. fix the application that wrote this file, to use linefeed only.
  2. replace the random newline characters with linefeed only.
The second one you could easily do, just add this line before the textscan call:
str = regexprep(str,'[\r\n]+','\n');
Note that after this you should also adjust the number of header lines to 12.
Johan Sebastian Diaz Tovar
Stephen, thank so much for your value help!
You were awesome!!!
The second one you told me works perfectly!
I will try for future measurements check the application to use linefeed only in the files. You clarify at all my doubts!
Big hug!

Sign in to comment.

More Answers (1)

Translated by