フィルターのクリア

How to replace all the strings of a csv file with NaN using textscan and "TreatAsEmpty" ?

2 ビュー (過去 30 日間)
Ziad Sliti
Ziad Sliti 2017 年 5 月 4 日
編集済み: dpb 2017 年 5 月 5 日
I'm using textscan to read huge csv file.
Theses files contain numbers and strings.
How to replace all the string with NaN using "TreatAsEmpty" ?
The location of the strings depend on the file and the strings are different for each file.
For example a csv file like this :
1 3 5 6 temp 56 d 3
4 f 45 5 6 H 4 5
How to obtain that ?
1 3 5 6 NaN 56 NaN 3
4 NaN 45 5 6 NaN 4 5
Thank you for your help !
Best,
  1 件のコメント
dpb
dpb 2017 年 5 月 4 日
Only way to do that with textscan would be to have a complete list of the strings contained in the file.

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2017 年 5 月 4 日
OTOH,
>> fid=fopen('ziad.txt');
>> fmt=repmat('%s',1,8);
>> c=textscan(fid,fmt,'collectoutput',1);
>> fid=fclose(fid);
>> data=str2double(c{:})
data =
1 3 5 6 NaN 56 NaN 3
4 NaN 45 5 6 NaN 4 5
>>
  2 件のコメント
Ziad Sliti
Ziad Sliti 2017 年 5 月 5 日
編集済み: Ziad Sliti 2017 年 5 月 5 日
Hello,
Thank you for your answer.
But I work with big csv files (6000 rows 1000 columns) and it takes too many time to read all as string.
What about using "TreatasEmpty" option of textscan ?
dpb
dpb 2017 年 5 月 5 日
編集済み: dpb 2017 年 5 月 5 日
That's not all that big; wouldn't think it too bad on time. What did it take?
As noted, you would have to have a complete list of all text fields in the file with an exact match for every one. Do you know that a priori?
I can't help but think even if did it wouldn't take quite a lot longer than the above.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by