How to read .txt file with numbers and texts?

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)

Use readtable(); it will figure everything out automatically.

14 Comments

Hello again, "readtable" doesn't work. This is the error:
Input must be a row vector of characters.
Please execute
class(SCRIPTfile)
SCRIPTfile
t = readtable(SCRIPTfile);
and show the results, including the full text of any error message
hi
Error using readtable (line 143) Input must be a row vector of characters. Errors. Thank you.
Error in collectData (line 41) t = readtable(SCRIPTfile);
I still need the information
class(SCRIPTfile)
SCRIPTfile
Hi,
I attached the file I need to read. And thats the error I got.
Error using readtable (line 143) Input must be a row vector of characters. Error in collectData (line 41) t = readtable(SCRIPTfile);
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);
Thank you very much for your concern. So I used:
SCRIPTfile=strcat(root,'1,3','.txt');
opt = detectImportOptions(SCRIPTfile, 'Delimiter', '\t');
opt.VariableNames(1:10) = {'label', 'mode', 'dur', 'win', 'iti', 'rdB', 'ldB', 'resp', 'type', 'SCRIPTfile'}
t = readtable(SCRIPTfile, opt);
But got his error.
Undefined function or variable 'detectImportOptions'.
Error in collectData (line 39)
opt = detectImportOptions(SCRIPTfile, 'Delimiter', '\t');
detectImportOptions() requires R2016b or later. Which version are you using?
Also, please show me the output of
disp(class(SCRIPTfile))
the output of disp(class(SCRIPTfile )) is cell. The version of MATLAB I am using is R2016a
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.
Hi, Thank you so much for your efforts. By Root I meant SCRIPTroot='C:\my_files\laurier\dissertation\stimuli\stimuliBothExperiements\english experiment\';
It still gives errors. This time: Cell contents reference from a non-cell array object.
Error in collectData (line 33) SCRIPTfile=strcat(SCRIPTroot{1},str2,'.txt');
Try
SCRIPTfile = strcat(SCRIPTroot,str2,'.txt');
if iscell(SCRIPTfile); SCRIPTfile = SCRIPTfile{1}; end
This is the script now. SCRIPTfile=strcat('C:\my_files\english experiment\'{1},'1,3','.txt'); if iscell(SCRIPTfile); SCRIPTfile = SCRIPTfile{1}; end 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);
Cell contents reference from a non-cell array object. Error in collectData (line 33) SCRIPTfile=strcat(SCRIPTroot{1},'1,3','.txt');
remove the {1} from that line.

Sign in to comment.

Tags

Asked:

on 30 Apr 2018

Commented:

on 1 May 2018

Community Treasure Hunt

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

Start Hunting!