Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
change in *.txt file

Subject: change in *.txt file

From: Khanh

Date: 25 Sep, 2011 05:38:14

Message: 1 of 4

I hope to get help/direction on this.

so I have a 'input.txt' file that contains a line such as:
command 1

Now, I want to change that value, '1', to something else, for example i=1:1:10, so replacing i for each run, I can save a new input.txt file. I can have
command 1
command 2
...
command 10
I'm not sure what exact matlab command for doing this. Could you suggest some?

Another question is....
I use fopen(fid,'w') but this create a new file. So I lost all other lines that I don't want to change beside the line 'command 1'. My solution for this is to copying all input.txt to input1.txt and copy all lines from input1.txt to input.txt when I fopen(input, 'w'). Then manually, individually change only the 'command 1' line. Do you know a better solution for this?

Thanks in advanced.

Subject: change in *.txt file

From: Sadik

Date: 26 Sep, 2011 01:42:10

Message: 2 of 4

Hello,

There could be better solutions, but one straight-forward solution is the following:

fread everything in the txt file to be of type char. This should give you a vector. Then, you can change the element of the vector corresponding to the character you would like to change. For example, if your txt file begins with command 1, then the character you would like to change is the 9th character [As in A(9) = '2';]. Once you make this change, you can then write this whole character array into a new file.

Best.

Subject: change in *.txt file

From: Steven_Lord

Date: 26 Sep, 2011 02:14:46

Message: 3 of 4



"Khanh " <kdcung@mtu.edu> wrote in message
news:j5meo5$4ff$1@newscl01ah.mathworks.com...
> I hope to get help/direction on this.
>
> so I have a 'input.txt' file that contains a line such as:
> command 1
>
> Now, I want to change that value, '1', to something else, for example
> i=1:1:10, so replacing i for each run, I can save a new input.txt file. I
> can have
> command 1
> command 2
> ...
> command 10
> I'm not sure what exact matlab command for doing this. Could you suggest
> some?
>
> Another question is....
> I use fopen(fid,'w') but this create a new file. So I lost all other lines
> that I don't want to change beside the line 'command 1'. My solution for
> this is to copying all input.txt to input1.txt and copy all lines from
> input1.txt to input.txt when I fopen(input, 'w'). Then manually,
> individually change only the 'command 1' line. Do you know a better
> solution for this?

Open the file in 'append' mode using the 'a' option.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: change in *.txt file

From: Khanh

Date: 26 Sep, 2011 03:10:28

Message: 4 of 4

Thank you for your suggestions. I have worked it out. I simply create a new copy of the file I want to edit, then use fwrite to copy/edit all the lines I want.


"Khanh" wrote in message <j5meo5$4ff$1@newscl01ah.mathworks.com>...
> I hope to get help/direction on this.
>
> so I have a 'input.txt' file that contains a line such as:
> command 1
>
> Now, I want to change that value, '1', to something else, for example i=1:1:10, so replacing i for each run, I can save a new input.txt file. I can have
> command 1
> command 2
> ...
> command 10
> I'm not sure what exact matlab command for doing this. Could you suggest some?
>
> Another question is....
> I use fopen(fid,'w') but this create a new file. So I lost all other lines that I don't want to change beside the line 'command 1'. My solution for this is to copying all input.txt to input1.txt and copy all lines from input1.txt to input.txt when I fopen(input, 'w'). Then manually, individually change only the 'command 1' line. Do you know a better solution for this?
>
> Thanks in advanced.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us