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

3 visualizaciones (últimos 30 días)
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.

Respuestas (2)

Vladimir Sovkov
Vladimir Sovkov el 27 de Dic. de 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.

Bhaskar R
Bhaskar R el 27 de Dic. de 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 comentarios
Hedaya Rizeq
Hedaya Rizeq el 29 de Dic. de 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)?
Stephen23
Stephen23 el 29 de Dic. de 2019
Editada: Stephen23 el 29 de Dic. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by