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?
採用された回答
fid = fopen('File.text','at');
7 件のコメント
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?
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.
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
"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.
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
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
2016 年 6 月 2 日
編集済み: Saeed Soltani
2016 年 6 月 2 日
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Text Data Preparation についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
