Write a substring in a Text file with existing string

Hi, I would like to write a word in the following of an existing string in text file, but with my code it deletes the whole and write the new word.
fid = fopen('File.text','w');
fprintf(fid,'%s', NewWord);
I just need to find end of line, and add the new word with only one space to the rest. Should I use textscan or?

 採用された回答

Stephen23
Stephen23 2016 年 5 月 30 日
編集済み: Stephen23 2016 年 5 月 30 日

0 投票

Change the fopen file permission from 'w' (write) to 'a' (append):
fid = fopen('File.text','at');

7 件のコメント

Saeed Soltani
Saeed Soltani 2016 年 5 月 30 日
Thanks. It works but when I parse the text file to catch all the words, there is an error:
“Attempt to reference field of non-structure array.”
And the new words in my array are shown like “[1x6 char]”
Array=
'ECUM ' 'ECU1 ' 'ECU2 ' [1x6 char] [1x8 char]
Do you probably know why?
Stephen23
Stephen23 2016 年 5 月 30 日
編集済み: Stephen23 2016 年 5 月 30 日
Sorry, but my crystal ball is not working today. You don't give any information about how you "parse the text file to catch all the words". If you actually show us what code you are using and tell us what you expect to happen then we can help you.
Saeed Soltani
Saeed Soltani 2016 年 5 月 30 日
First I have to say, not only it does not make the space, but also some time it goes to the next line. That must not happen because my parsing pattern will affect.
About what I am doing:
I have a dynamic GUI tool, it gets new word and run some functions, and additionally it must insert this name to a text file. Then from other part of my GUI I have a listbox that must be updated. It parses the whole text file and base on a pattern (space + \ + Word) extract all “Words” to the list box. Actually I want to update my listbox based on the updated text file. Here is the code (parsing part) of push button which update the listbox:
function pushbutton1_Callback(hObject, eventdata, handles)
filecontent = fileread('ECUNames.text');
scchildren = regexp(filecontent, '(?<=/)\w+\s', 'match')
A = length(scchildren);
listboxItems1 = scchildren(1:A);
set(handles.listbox1,'String', listboxItems1 );
One thing that I noticed, the error I told you, only happens when I click the button directly from the GUI. But If I run the code (.m file) it updates the listbox of GUI without error.
Thank you in advance for your time and help
Stephen23
Stephen23 2016 年 5 月 30 日
編集済み: Stephen23 2016 年 5 月 30 日
"it does not make the space" you can easily add this in your fprintf call:
fprintf(fid,' %s', NewWord);
Newlines are not added by this code. If there are newlines in the file then they are part of the strings that are being passed to fprintf, or were in the file beforehand. You might also be getting confused by what a text editor displays: word wrapping is not the same as a newline character.
MATLAB does not display infinite amount of information in the variable summary: when the content of cell is a non-scalar numeric, or a string longer than some length, then it is displayed only with meta-information (its class and size) instead of its content:
>> C = {'abc','defghijklmnopqrstuvwxyz','abcdefgh','i','jklmn','opqrstuvwxyz'}
C =
'abc' [1x23 char] 'abcdefgh' 'i' 'jklmn' 'opqrstuvwxyz'
>> C{2} % See, the string is still there!
ans =
defghijklmnopqrstuvwxyz
Note that a neater and faster way of passing data between GUI callbacks is to use guidata. You will find the recommended methods described here:
I have no idea why "Attempt to reference field of non-structure array" happens, because you did not give us the complete error message.
Saeed Soltani
Saeed Soltani 2016 年 5 月 31 日
Thank you. Now I have the text structure that I wanted. Also the error is solved. I think the problem was related to the text structure as well. But I am getting new problem. I will write it here and also make it as a new question too, may be in the future would be the problem of someone else. The problem is: I have a GUI which get new word as input, then parse my Matlab file (.m) and replace the word with an especial word. Then I use the updated Matlab file. Although the file is updated (the corresponding word is replaced), some time when I run the code, it evaluate based on the last word (which is false and makes error). The interesting part is, when I click on the error line (which says for ex. This “word” is not existed), it goes through my code and shows the right word. It means that it is already replaced but it is still using the last word. Do you have any idea?
Stephen23
Stephen23 2016 年 5 月 31 日
MATLAB reads fucntion files once and then stores them in memory. This makes them faster (and I guess some JIT optimization occur too). What you are trying to do is to dynamically change MATLAB code, which is not a recommended way of using MATLAB, because it is slow and buggy.
Saeed Soltani
Saeed Soltani 2016 年 6 月 2 日
編集済み: Saeed Soltani 2016 年 6 月 2 日
I got a solution here

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeText Data Preparation についてさらに検索

質問済み:

2016 年 5 月 30 日

編集済み:

2016 年 6 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by