extract data un 2D array to create 3d array

Hello,
I'm new on Matlab, and I would like to create a 3D array by extracting data in a 2D array.
I Have an array, containing eye data during multiple Trials, which size is 6942173x1cell (the attached file is smaller in order to attached it). Each trial start with 'MSG Number... Recenter Cue Event'. I would like to extract data of each trial and to create a 3D array containing each trial data in different pages.
I created this code but the loop doesn't work, it extracts only the first trial and doesn't create a 3D array.
idx1 = find(~cellfun('isempty',strfind(Edf,'Recenter Cue Event')));
for i=1:1:size(Edf)
for k=1:size(idx1)
if i==idx1(k)
Edf_Trials(:,:,k)=Edf(idx1(k):idx1(k+1)-1);
else
i=i+1;
end
end
end
Thank you for your help
Alice

9 Comments

@alice dormeuil: could you please upload the original file as well?
The file is too heavy. I put it in a google drive : https://drive.google.com/open?id=1yYKyAWl7Yhar1maPZpjQEf1HjAhcZ2Kf The 'matlab' file is the file with all the data, and the '.asc' file is the file I obtained after my experiment. I extract the data with the code :
[filename,file_path] = uigetfile(['/Users/maria/Documents/*.asc']...
,'Choose MATLAB File to Process');
fid = fopen([file_path filename],'r');
Edf = textscan(fid,'%s ','Delimiter', '\n','EndOfLine','\r\n');
Edf = Edf{1};
fclose(fid);
What is the value of idx1()?
idx1 contains the rows of Edf file, where each trial starts (the start of each trial contains 'MSG Number... Recenter Cue Event'). I attach the idx1 array too.
Try removing the 'else' portion of your if statement.
I have exactly the same result.
Try putting a pause option (the red circle that you can put on the left side of the line) on the Edf_Trials(:,:,k) line and check what values of i and k you're getting. It might be that for some reason they just don't line up.
i = 12968 and k = 1 so the code is running correctly for the first part. As I removed the else portion I have an error message 'Subscripted assignment dimension mismatch'.
I'm assuming the error message occurs on this line:
Edf_Trials(:,:,k)=Edf(idx1(k):idx1(k+1)-1);
If so, then it is most likely because you can't have k+1 when k=size(idx1). I would suggest writing an if statement that looks for this condition and just tells the Edf range to go to the end. This can be part of your current if statement, but the condition needs to come before what you have now.
if k == size(idx1,1)
...
elseif i == idx1(k)
...
end
Also, I would suggest it is good coding practice to specify a single dimension when using size() or to use two variables as outputs.
[row, col] = size(idx1);
By making those two changes your code seemed to run fine for me with a bunch of dummy data. This makes me think that the bug is in the recognition of 'Recenter Cue Event'. What is the value of idx1 when you run it? (I know you included a sample of your data, and thank you, but for personal paranoia reasons I don't download things off this site.)

Sign in to comment.

 Accepted Answer

Hi, Thanks for your suggestion which help me to find the solution. The problem was :

  • the size of the trials was different among the trial, so I have to create a cell array with {}
  • effectively the other problem was k+1 doesn't exist when k=size(idx1).

the new code I made is :

idx1 = find(~cellfun('isempty',strfind(Edf,'Recenter Cue Event')));
for i=1:1:size(Edf)
    for k=1:(size(idx1)-1)
        if i==idx1(k)
            Edf_Trials(:,1,k)={Edf(idx1(k):idx1(k+1)-1)};
        end
    end
end
k=size(idx1);
Edf_Trials(:,1,k)={Edf(idx1(k):end)};

And It's perfectly working.

Thanks again for your help.

Alice

More Answers (0)

Categories

Find more on Data Import and Analysis 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!