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

2 次查看(过去 30 天)
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.

回答(2 个)

Vladimir Sovkov
Vladimir Sovkov 2019-12-27
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.

Bhaskar R
Bhaskar R 2019-12-27
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 个评论
Hedaya Rizeq
Hedaya Rizeq 2019-12-29
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)?
Stephen23
Stephen23 2019-12-29
编辑:Stephen23 2019-12-29
"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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by