Removing comas from the text file
9 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Rajanya
on 28 Jan 2025 at 4:55
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 Comments
Walter Roberson
on 28 Jan 2025 at 5:19
Edited: Walter Roberson
on 28 Jan 2025 at 5:22
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 Comments
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!