Getting NaN when converting string to number
4 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194149/image.jpeg)
Hi, I am reading the first line of 4 text files and trying to convert the second column in each file name to a number as i have time stored in the second column. When i convert the second row of elements to number, i am getting NaN. Screen shot of first line of a text file is attached. Any help to solve this will be appreciated.
first_row_of_text_file = cell(number_of_files,1);
for ii = 1:number_of_files
fileID = fopen(filenames{ii},'r');
varNames = textscan(fileID, format, 1, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'TextType', 'string', 'ReturnOnError', false);
first_row_of_text_file{ii} = str2double(varNames(:,2));
end
8 个评论
jonas
2018-8-21
编辑:jonas
2018-8-21
Then you can convert the string to either a duration or datetime. After subtraction, the format will be duration regardless. In this case I'd suggest going for duration directly as you don't have any date in the same cell.
I still don't understand why you don't use the timetable that I helped you build some days ago. It would be extremely easy to calculate the time difference.
回答(1 个)
Kuifeng Zhao
2018-8-21
I suggest change the brackets (),[], into comma ',', and then try to read the data again. Anyway, you can try the function and make necessary changes: importdata('data.txt').
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!