trying to obtain data from a txt file but one column has negative values and ignores the - sign and leaves it as +

3 次查看(过去 30 天)
str = fileread('Wello.txt');
nums = cellfun(@str2double, regexp(str, '([\d.,]+)', 'match'));
DATA = reshape(nums, 3, [])';
this is the code above.
txt file is like this::
H G J
245 54 -45
output after code:
245 54 45

采纳的回答

Ameer Hamza
Ameer Hamza 2020-9-30
编辑:Ameer Hamza 2020-9-30
Why use regular expression here. Just use readmatrix()
M = readmatrix('data.txt');
Result
>> M
M =
245 54 -45
For the regular expression in your question, you missed the minus (-) sign
str = fileread('data.txt');
nums = cellfun(@str2double, regexp(str, '(-?[\d.,]+)', 'match'));
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by