replce all hyphens(-) in .txt file with 0

Hi
I am having numeric data in the form of 10*8 array in .txt file. The data is having 2 rows of hyphens which are causing problem in reading this data. I want to raplace hyphen(-) with zero (0).
Again one thing as data is having negative numbers also, so we have to replace -,(hyphen followed by comma which is delimiter) with 0, sothat negative numbers will remain intact.
So how can I do this replacement? Something like 'regexprep'.. but I want to replace all hyphens in .txt file.
Thanks in advance.

3 comentarios

Fangjun Jiang
Fangjun Jiang el 15 de Sept. de 2011
Provide an example and then we can start from there.
Walter Roberson
Walter Roberson el 15 de Sept. de 2011
To cross-check: the hyphen-comma should be replaced by zero-comma ?
Nikhil CD
Nikhil CD el 16 de Sept. de 2011
@ Walter: yes you are absolutely right..

Iniciar sesión para comentar.

 Respuesta aceptada

Grzegorz Knor
Grzegorz Knor el 15 de Sept. de 2011
Suppose that you have test file:
1.0-, 2.0
1.2-, -2.3
-1.4-, 2.6
code:
fid1 = fopen('test','r');
fid2 = fopen('test_copy','w');
tline = fgetl(fid1);
while ischar(tline)
tline = strrep(tline,'-,','0,');
fprintf(fid2,'%s\n',tline);
tline = fgetl(fid1);
end
fclose(fid1);
fclose(fid2);

2 comentarios

Nikhil CD
Nikhil CD el 16 de Sept. de 2011
Thanks for response.
But my data is not like you have taken. My data looks like
1, 2.3, 37, -, -4, 2
2, -1.7, -, -, 25, 6
3, -12, -, -, -5.4, 2
Nikhil CD
Nikhil CD el 20 de Sept. de 2011
After some modification your code works perfectly well for my need..
Thank you.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 15 de Sept. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by