Formatting or encoding issue using fprintf

Hi everyone. I am writing a script that is intended to automatically update information for me but I am having trouble towards the end where the string is saved to a .txt file.
The code that I have so far is:
S=regexprep('base string','keyword','new word')
G=char(S)
M=fopen('file.txt','wt')
fprintf(M,'%s',G)
fclose(M)
I am using regexprep to alter the original file and replace a word within. However, trying to use fprintf directly from the regexprep gives me the error "function is not defined for 'cell' inputs"
To avoid this error, I used the char function to convert the cell S into a character array. However, when G (the result of the char function) is saved to a text file, it is clearly formatted incorrectly. [An excerpt of the saved file: "b('-t'*(a('-t'*(eeoo"]
I can not seem to find any other functions that either do not produce an error in the fprintf function or give me gibberish in my saved file.
Any help would be greatly appreciated. Thanks for reading this long post at the least!
~Sarah :)

 採用された回答

Walter Roberson
Walter Roberson 2011 年 9 月 2 日

1 投票

fprintf(M,'%s',S{:}) %without needing to convert to char()
Or,
fprintf(M,'%s',G.')

4 件のコメント

Sarah
Sarah 2011 年 9 月 2 日
Walter you are a fantastic help! Thanks again!
Sarah
Sarah 2011 年 9 月 2 日
Hello again,
Apparently there is still a slight format issue. The file is saved in a manner that is legible, so it does not produce an error, but the spacing is incorrect and different from the source file from which it originated. Because of this, the command does not execute properly.
Is there a way to ensure that the file returns to it's exact original format?
Any direction would be really appreciated. Thanks!
~Sarah :)
Walter Roberson
Walter Roberson 2011 年 9 月 2 日
A) don't use textread, use textscan
B) Use '%[^\n]' as the format instead of '%s'
Sarah
Sarah 2011 年 9 月 5 日
When I use:
x=fopen('file.txt','wt')
y=textscan(x,'%[^\n]')
instead of:
z=textread('file.txt')
the result, y, is a cell array intead of a string and produces an error when I try to utilize it inside regexprep. Is there a conversion that needs to take place am I misunderstanding something? I can not seem to find a function to convert a cell array into a string.
Any help is appreciated. Thanks.
~Sarah :)

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

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 2 日

0 投票

S=regexprep('base string','keyword','new word') shouldn't return a cell array if your input is a string as 'base string'.
Try to work out your regexprep() first. Using debugging mode to check the result of your regexprep() before writing to the text file.
Another function is strrep().

1 件のコメント

Sarah
Sarah 2011 年 9 月 2 日
Thanks for your prompt reply!
My 'base string' is a variable defined by: J=textread('filename.txt','%s')
S=regexprep(J,'keyword',[another variable like J])
This may be a silly question, but I am not familiar with debugging mode, is it a function or a feature?
strrep in place of regexprep returned the same "function is not defined for 'cell' inputs" error.
Thanks for your help! :)

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

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by