textscan doesn't work well when I read 2 lines batch.

using code below I can't read third number of data I think encoding newline has problem. interestingly when I make test data by typing with same form it works well. but when I deal with raw data it doesn't work. what's the problem?
data :
842428 / Present Voltage = 229
Present Velocity = 8973
842863 / Present Voltage = 228
Present Velocity = 8997
code :
clc; clear; close all;
fid = fopen("23V, 0.17~0.20A.txt","r");
data = textscan(fid,'%d / Present Voltage = %d Present Velocity = %d');
fclose(fid);
result :
data = 842428, 229, []

1 件のコメント

per isakson
per isakson 2018 年 5 月 3 日
Try this
str = sprintf( '842428 / Present Voltage = 229\n Present Velocity = 8973\n' );
str = [ str, str ];
data = textscan( str,'%d / Present Voltage = %d\n Present Velocity = %d\n')
data =
[2x1 int32] [2x1 int32] [2x1 int32]

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

 採用された回答

Akira Agata
Akira Agata 2018 年 5 月 3 日
編集済み: Akira Agata 2018 年 5 月 3 日

1 投票

Seems that you need to add newline code (\n) or carriage return + newline codes (\r\n), like:
data = textscan(fid,'%d / Present Voltage = %d\r\n Present Velocity = %d');
In my PC environment (Windows10 + MATLAB R2018a), it returns 1-by-3 cell array.
>> data
data =
1×3 cell array
{2×1 int32} {2×1 int32} {2×1 int32}

1 件のコメント

Dong Gi Sung
Dong Gi Sung 2018 年 5 月 3 日
Thank you so much! '\r' this was the problem!!

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by