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?
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
