MATLAB: Str2double conversion…

str2double

clc;
fid = fopen('fileName','r');
allText = textscan(fid,'%s','delimiter','\n')
allText = strfind(allText{1}, 'Num_X=');
strv =(find(~cellfun('isempty', allText)));% find position of Num_X
str = cell (11,1);
fseek(fid,0,-1);
for k = 1:11
str{k}= fgetl(fid);
end
W = str2double(str{strv});% trying to obtain number in that string.
Returns NaN .Why?

Best Answer

Because str{strv} will be a single character and equal to 'N' assuming the string 'Num_X=' is in the text in the first line; otherwise it'll be whatever character happens to be in that position in the line. fgetl returns a character array; subscripting it with a single index returns only a single character, not a full string as does a reference to a cell string. Of course, str2double and friends aren't cellstring aware.
textscan is probably a better approach to reading the file; let's see a small subset of the file format...