getting only numbers from an 88x3 cell array.

I have imported my data from a text file which is attached below.I have successfully imported the data into matlab but now i want to extract specific data labeled under the Calculated Reaction time so far and now the Raw reaction time. The number of calculated reaction time readings can change depending on the subject who was taking the test. I am trying to have the code check for numbers in all three columns of each row and if the response is missing or not recorded to discard that reading and keep going on with checking if it the remainder rows are all numerical. I also want it to truncate when it hits the '''''''' before the Raw reaction times. My code, so far, is as follows:
% feed the data file here with the reaction times.
[filename] = uigetfile('*', 'Select the data file')
% This function imports the reaction data from the file
import_data = importdata(filename)
[data] = import_data.data
fid = fopen(filename,'r')
scan_init_cell = textscan(fid, '%s %s %s', 'headerlines',18, 'CollectOutput', 1)
%scan_init = cell2mat(scan_init_cell{:})
for 1:size(scan_init_cell{:},1):
cellfun(@isnumeric(scan_init_cell{:},1)
fclose(fid)

 Accepted Answer

This will read your file and do what you want. If there are string responses other than {'Responsed to Nothing','Missed Response'}, you will have to include them in the 'TreatAsEmpty' cell in the textscan call.
fidi = fopen('Sanwal Yousaf USE_THIS.txt','r');
scan_init_cell = textscan(fidi, '%f%f%f', 'HeaderLines',20, 'CollectOutput',1, 'TreatAsEmpty',{'Responsed to Nothing','Missed Response'});
fclose(fidi);
scan_init = cell2mat(scan_init_cell);
scan_init = scan_init(~isnan(scan_init(:,3)),:); % Omit Rows With ‘NaN’ In Column #3

8 Comments

are they supposed to be three scan_init in the last line?
Yes, there are.
The reason is that I already assigned ‘scan_init’, so I am now reassigning it as only those rows with no NaN values in Column #3.
This expression:
~isnan(scan_init(:,3))
defines the rows that do not have NaN values in Column #3 as a logical vector. (Run it individually as a separate line to see how it works.) I then reassign ‘scan_init’ as only those rows of it that do not contain NaN values in Column #3.
I could have broken it out into two lines:
no_NaN_in_Col_3 = ~isnan(scan_init(:,3));
scan_init = scan_init(no_NaN_in_Col_3, :);
but chose not to.
See the documentation on logical indexing and matrix indexing for a full explanation.
Error using cell2mat (line 52) CELL2MAT does not support cell arrays containing cell arrays or objects.
Error in parser_mine (line 9) scan_init = cell2mat(scan_init_cell)
This the error that i get when i run this code.
It ran for me without error using the file you provided (in R2015b). There could be version differences if you are using an earlier version. What version of MATLAB are you using?
Did it run for you with my code and the file you attached here?
If you tried to read another file, attach it as well, and I’ll do my best to see what the problem is with it.
I found a way to make it work. I used
scan_init = [scan_init_cell{:}]
instead of cell2mat to import the data. Thanks for the help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!