Removing comas from the text file

9 visualizzazioni (ultimi 30 giorni)
Anusha
Anusha il 4 Dic 2013
Modificato: Walter Roberson il 28 Gen 2025 alle 5:22
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

Risposte (2)

Rajanya
Rajanya il 28 Gen 2025 alle 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.

Walter Roberson
Walter Roberson il 28 Gen 2025 alle 5:19
Modificato: Walter Roberson il 28 Gen 2025 alle 5:22
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.

Community Treasure Hunt

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

Start Hunting!

Translated by