Getting NaN when converting string to number

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 件のコメント

Stephen23
Stephen23 2018 年 8 月 21 日
@Hari Krishnan: please upload a sample file by clicking the paperclip button.
Hari krishnan
Hari krishnan 2018 年 8 月 21 日
added the file.
jonas
jonas 2018 年 8 月 21 日
How do you want to convert 13:00:02.754000 to a number? What is your desired output?
Hari krishnan
Hari krishnan 2018 年 8 月 21 日
I just want to convert this string to a numeric form so that i can do arithmetic operations.
jonas
jonas 2018 年 8 月 21 日
編集済み: jonas 2018 年 8 月 21 日
How do you write 13:00:02.754000 in numeric form? I mean, let's say:
A=13:00:02.754000
out=A/2
What is the expected answer?
You may want to look into converting it to a datetime or datenum (double)
Finally, in your previous questions (see e.g. this one ), you already converted the entire textfile to a MATLAB table. Why are you even still working with this text file?
Walter Roberson
Walter Roberson 2018 年 8 月 21 日
Or into a duration.
Hari krishnan
Hari krishnan 2018 年 8 月 21 日
What i want to do is to take the time from the first row and substract this from the time in the successive rows. So i can get a column with increasing time rather than the time stamp itself.
jonas
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
Kuifeng Zhao 2018 年 8 月 21 日

0 投票

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').

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2018 年 8 月 21 日

編集済み:

2018 年 8 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by