Removing comas from the text file

9 visualizaciones (últimos 30 días)
Anusha
Anusha el 4 de Dic. de 2013
Editada: Walter Roberson el 28 de En. de 2025 a las 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

Respuestas (2)

Rajanya
Rajanya el 28 de En. de 2025 a las 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 el 28 de En. de 2025 a las 5:19
Editada: Walter Roberson el 28 de En. de 2025 a las 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.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by