Filter löschen
Filter löschen

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

8 Ansichten (letzte 30 Tage)
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::
245 54 -45
output after code:
245 54 45

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 30 Sep. 2020
Bearbeitet: Ameer Hamza am 30 Sep. 2020
Why use regular expression here. Just use readmatrix()
M = readmatrix('data.txt');
>> 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 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)


Mehr zu Tables finden Sie in Help Center und File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by