MATLAB: Error: Index exceeds matrix dimensions

indexing

fid = fopen('cDNA_1(1).tsv', 'r');
ftext= textscan(fid, '%q%q%q%q%q%q');
ref = ftext(4); sz_ref = size(ref);
alt = ftext(5); sz_alt = size(alt);
count = 0;
for i = 1:sz_ref(1,1)
ref_i = ref{i};
idx1 = strmatch('A',ref_i, 'exact');
for j = 1:idx1(1,1)
alt_i = alt{j};
if isequal(alt_i,'T')
A_T = count+1;
end
end
end
error in: for j = 1:idx1(1,1)
alt_i = alt{j};

Best Answer

Your code
idx1 = strmatch('A',ref_i, 'exact');
for j = 1:idx1(1,1)
fails if the strmatch returns empty, because the second line assumes that idx1 will have at least one element.
Your code
ref = ftext(4); sz_ref = size(ref);
alt = ftext(5); sz_alt = size(alt);
is wrong. You need
ref = ftext{4}; sz_ref = size(ref);
alt = ftext{5}; sz_alt = size(alt);
ftext is a cell array with as many columns as you have format items; when you use () indexing on it you get back a 1x1 cell array. You are interested in the content of the column, so you need {} indexing.
Related Question