MATLAB: Trying to convert string to number but the last number is not converting. What am I doing wrong in the code? The problem is B(j)=str2num(B(j)) row.

nb

function [n_prn]=findPRN(SATXYZ2A)
SAT=[SATXYZ2A(:,[12:221])];
[Row,Col] = size(SAT);
%Eliminate data which is not GPS
for i = 1:10:Col
String_temp = (SAT{1,i});
if not(strcmp(String_temp,'GPS'))
break;
end
end
SAT = [SAT(:,[1:i-1])];
A = dataset2cell (SAT);
A(1,:)=[];
[Row,Col] = size(A);
B=[A(1,[2:10:Col])];
[Row,Col] = size(B);
for j=1:Col
if ischar(B{j})
B(j)= str2num(B(j));
end
end
B = cell2mat(B);
n_prn=transpose(B);
end

Best Answer

For a start it should be
B{j} = str2num(B{j});
and I would recommend using str2double instead of str2num. Given the right(?) input str2num could format your hard drive or do all kind of nefarious things.
B{j} = str2double(B{j});
In any case, are you sure that B{j} actually contains something that can be decoded as a number when it is text?
Note:
[Row,Col] = size(A);
B=[A(1,[2:10:Col])];
None of the square brackets are needed in the second line and the whole lot could be written more simply as
B = A(1, 2:10:end);
Related Question