How to read .txt file with numbers and texts?
Show older comments
Hello. I have a .txt file including numbers and texts separated by tabs. I have tried dlmread, csvread, etc but not still able to get the file content as a matrix. I do not know what is wrong. I appreciate your help.
file=strcat('C:\my_files\','1,3','.txt');
SCRIPTfile = char(file);
dataSCRIPT=dlmread(SCRIPTfile);
Answers (1)
Walter Roberson
on 30 Apr 2018
0 votes
Use readtable(); it will figure everything out automatically.
Or you could make a small modification to the code I posted at https://www.mathworks.com/matlabcentral/answers/285186-importing-data-without-knowing-number-of-columns#comment_368710
14 Comments
Elaheh
on 30 Apr 2018
Walter Roberson
on 30 Apr 2018
Please execute
class(SCRIPTfile)
SCRIPTfile
t = readtable(SCRIPTfile);
and show the results, including the full text of any error message
Elaheh
on 30 Apr 2018
Walter Roberson
on 30 Apr 2018
I still need the information
class(SCRIPTfile)
SCRIPTfile
Elaheh
on 30 Apr 2018
Walter Roberson
on 30 Apr 2018
if ~ischar(SCRIPTfile); error('SCRIPTFile is wrong data type, expected character vector, got "%s"', class(SCRIPTfile)); end
opt = detectImportOptions(file, 'Delimiter', '\t');
opt.VariableNames(1:10) = {'label', 'mode', 'dur', 'win', 'iti', 'rdB', 'ldB', 'resp', 'type', 'filename'}
t = readtable(SCRIPTfile, opt);
Elaheh
on 30 Apr 2018
Edited: Walter Roberson
on 30 Apr 2018
Walter Roberson
on 30 Apr 2018
detectImportOptions() requires R2016b or later. Which version are you using?
Also, please show me the output of
disp(class(SCRIPTfile))
Elaheh
on 1 May 2018
Walter Roberson
on 1 May 2018
Your variable root is not a character vector; it must be a cell array of character vectors. You should be using
SCRIPTfile=strcat(root{1},'1,3','.txt');
Since you do not have detectImportOptions, use:
fmts = repmat({'%f'}, 1, 11);
fmts([2,4, 10]) = {'%[^\t]'};
fmts(11) = {'%*[^\n]'};
fmt = strjoin(fmts,'');
fid = fopen(SCRIPTfile, 'rt');
datacell = textscan(fid, fmt, 'HeaderLines', 3, 'Delimiter', '\t');
fclose(fid);
datacell will now be a cell array of 10 entries, one per column of input. Column 2 will be a cell array of character vectors, corresponding to the word 'IMAGE'. Column 10 will be a cell array of character vectors, corresponding to the file name.
Column 4 will be a cell array of character vectors. This corresponds to the win column, which is a column that mostly contains numbers but also contains character entries such as WAIT . As I do not know what you want done with those WAIT entries, I left the entire column as text instead of trying to do a conversion.
Elaheh
on 1 May 2018
Walter Roberson
on 1 May 2018
Try
SCRIPTfile = strcat(SCRIPTroot,str2,'.txt');
if iscell(SCRIPTfile); SCRIPTfile = SCRIPTfile{1}; end
Elaheh
on 1 May 2018
Walter Roberson
on 1 May 2018
remove the {1} from that line.
Categories
Find more on Simulink 3D Animation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!