Copy text file into different array depending on character in the first cell

8 views (last 30 days)
I have a text file with different information in each line:
  • the first 9 lines represent the experiment, date, condition and name of the subject.
  • The line 10 contain the header of behavioural data
  • the others contain the behavioural data which always start with the name of the subject, then eye mouvement data
  • which are separated in 'all saccades' which always start with 'All Saccades' and microsaccades which start with 'microsaccades'.
I would like to create 3 arrays :
  • one with only behavioural data
  • one with All Saccades data
  • one with micro Saccades data.
The dimension of these array will contain the same amount of lines but different number of columns.
I'm new in Matlab, and i'm not able to find the correct code.
I inser the text file and the code I made.
Thanks for your help
Alice
clc
clear
close
% Fichier à ouvrir
[filename, file_path] = uigetfile(['C:\Brandy\Pilot\*.txt'],'Choose MATLAB File to Process');
fid = fopen([file_path filename],'rt');
if fid < 0
fprintf('error opening file\n');
return;
end
%lecture du fichier
line_number = 1;
oneline{line_number} = fgetl(fid);
while ischar(oneline{line_number})
line_number = line_number + 1;
oneline{line_number} = fgetl(fid);
end
fclose(fid);
%Séparation des lignes
Title_1 = oneline{2};
Title_2 = oneline{3};
Date = oneline{4};
Time = oneline{5};
Condition = oneline{7};
SubjName = oneline{8};
Label = oneline{10};
Labels = strsplit(Label,';');
for i=1:line_number
Lines{i}=char(oneline{i});
end
j=line_number-10;
for ii = 1:j
k=ii+10;
[Index{ii},~,~,n] = sscanf(Lines{k},"%s");
end
Index_1=Index';
Data = regexp(Index_1, ';', 'split');
%Formation d'un nouveau tableau
contents{1} = Labels;
for m=1:4336
if ischar(Data{m,1}{17})==1
end
end

Accepted Answer

Akira Agata
Akira Agata on 27 Mar 2018
Edited: Akira Agata on 27 Mar 2018
How about the following script? In this example, I assumed 'behavioural', 'all saccades' and 'micro saccades' data line start with 'Alice', 'All Saccades' and 'Micro Saccades', respectively.
% Read data file
fid = fopen('16_03_18 16h30.txt','r');
str = textscan(fid,'%s',...
'Delimiter', '\n',...
'EndOfLine', '\r\n');
str = str{1};
fclose(fid);
% 1. Extract behavioural data
idx1 = startsWith(str,'Alice');
c1 = cellfun(@(x) split(x,';')',str(idx1),'UniformOutput',false);
data1 = vertcat(c1{:});
% 2. Extract all saccades data
idx2 = startsWith(str,'All Saccades');
c2 = cellfun(@(x) split(x,';')',str(idx2),'UniformOutput',false);
data2 = vertcat(c2{:});
% 3. Extract micro saccades data
idx3 = startsWith(str,'Micro Saccade');
c3 = cellfun(@(x) split(x,';')',str(idx3),'UniformOutput',false);
data3 = vertcat(c3{:});

More Answers (0)

Categories

Find more on Genomics and Next Generation Sequencing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!