Removing comas from the text file

I have a text file containing some numbers separated with coma.How can I remove those comas. Example: data.txt={1,2,3,4.........}.Is there any method to remove those comas?
My text file when loaded to matlab is 2517x1001 matrix.
Kindly respond

답변 (2개)

Rajanya
Rajanya 2025년 1월 28일

0 개 추천

You can use 'strrep' to replace the comas in the text file with blank spaces.
Refer to the documentation to know more about the function and the ways to use it by executing the following command from MATLAB Command Window -
doc strrep
Thanks.
Walter Roberson
Walter Roberson 2025년 1월 28일
편집: Walter Roberson 2025년 1월 28일

0 개 추천

If you have a file of text that looks like
1,2,3,4,5
6,7,8,9,10
then it is enough to load the file.
data = load('data.txt');
MATLAB's load() command will automatically detect the commans and parse the data correctly.
However, if your text file contains () or {} such as
{1,2,3,4,5}
{6,7,8,9,10}
then you need to do something like
S = fileread('data.txt');
S = regexprep(S, {'{', '}'}, {'', ''});
after which you can use
data = num2str(S);
There are other routines such as readmatrix() that are also suitable for cases such as these, but readmatrix() was not introduced until R2019a, long after this question was asked.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2013년 12월 4일

편집:

2025년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by