MATLAB Answers

New to Matlab- How to import a text file for analysis that contains numbers and characters?

3 views (last 30 days)
Hedaya Rizeq
Hedaya Rizeq on 27 Dec 2019
Edited: Stephen Cobeldick on 29 Dec 2019
I have a text file that has columns that contain both numbers and characters (attached). What function can I use to be able to read the file for further analysis? I need to calculate the average reaction times based the types of stimuli.

  0 Comments

Sign in to comment.

Answers (2)

Vladimir Sovkov
Vladimir Sovkov on 27 Dec 2019
If you need it only once (or few times), use the Import utility of Matlab (Home\Import Data - a green downward arrow); chose the output type either "table" or "numeric array" depending on what you want. To work with this kind of files repeatedly, you should write a program using file operations (fopen, fread, fclose, ...) and string processing tools. Check the Matlab documentation.

  0 Comments

Sign in to comment.


Bhaskar R
Bhaskar R on 27 Dec 2019
Using textscan command you can get the required data from the input text file, data would be extracted in the cell data type
try this to extract the data from input file
fmt = '%d\t%d\t%s\t%d\t%d\t%d\t%s\t%d\t%d\t%s\t%s'; % format of the text file
f = fopen('T001_1.txt', 'rt'); % open input file
% get the file data to variable data(cell data type)
data = textscan(f, fmt ,'HeaderLines',1,'CollectOutput',true);
fclose(f); % close file identifier
%% do your calculation on extracted "data"

  5 Comments

Show 2 older comments
Walter Roberson
Walter Roberson on 29 Dec 2019
When you use CollectOutput, all of the consecutive items with the same format are gathered into one output. So %d%d is put into one array, then %s into a second, then %d%d%d into a third, %s a fourth, %d a fifth, then %s%s into the sixth.
It might be easier for you to remove the CollectOutput option. And you should probably change filedata(:,1) to filedata{1}, filedata(:,2) to filedata{2} and so on.
fprintf(fid1,'%s \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t %g \t n',sub_number, sub_condition, Avg_MDC_LB, Avg_MDC_RB, Avg_MDC_LG, Avg_MDC_RG, Avg_DC_LB, Avg_DC_RB, Avg_DC_LG, Avg_DC_RG, Percent_Error);
That is not going to work for you because of the way that fprintf goes down columns of data. You should have a look at compose()
Hedaya Rizeq
Hedaya Rizeq on 29 Dec 2019
Thank you, this was very helpful. Also, I haven't realized how tricky it is to perform simple math with numbers stored in cell arrays. Any tips on how I can calculate the mean of (Response_ms - Stimulus_ms)?
Stephen Cobeldick
Stephen Cobeldick on 29 Dec 2019
"How can this be fixed?"
By reading the comment that i wrote two days ago, where I recommended removing the 'CollectOutput' option, and explained why.
"I haven't realized how tricky it is to perform simple math with numbers stored in cell arrays. Any tips on how I can calculate the mean of"
By reading Walter Roberson's comment from four horus ago, which includes the advice to change your cell array indexing (from parentheses to curly braces):
CueSecond = fileData{1}; % Get the CONTENT of the 1st cell
CueMillisecond = fileData{2}; % Get the CONTENT of the 2nd cell
etc.
You should also revise cell array indexing

Sign in to comment.

Sign in to answer this question.