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

2 ビュー (過去 30 日間)
Hedaya Rizeq
Hedaya Rizeq 2019 年 12 月 27 日
編集済み: Stephen23 2019 年 12 月 29 日
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 Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by