Removing comas from the text file
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (2개)
Rajanya
2025년 1월 28일
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.
댓글 수: 0
Walter Roberson
2025년 1월 28일
편집: Walter Roberson
2025년 1월 28일
If you have a file of text that looks like
1,2,3,4,5
6,7,8,9,10
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!