How to save text from editbox to a text file ?

1 view (last 30 days)
i use this code :
fid = fopen('text.txt','wt');
txt = get(findobj('Tag','text10'),'String');
fprintf(fid,'%s',txt);
set(findobj('Tag','text4'),'String',txt);
fclose(fid)
the problem is that my editbox is muli-sentence and the saving goes from the first char of every sentence and then 2nd and 3rd
so if my text s
abc
efg
the text in the text file is
aebfcg
why is that ? need help asap!. thanks!

Answers (1)

Walter Roberson
Walter Roberson on 13 Jun 2015
You stored the text in the control as an array of character rather than a cell array of string. What you got back from the get() was an array of characters. And when you format any kind of array, including array of characters, the object is retrieved in row-major order, which is to say down the first column then down the second column and so on.
fprintf(fid,'%s',txt.');
would fix up the order for you. At that point you would notice that your text would come out as
abcdef
rather than as
abc
def
You did not ask it to put in any newlines, and you will find that there isn't any mechanism to be able to automatically recognize the end of a row (or column) and move on to the next format item such as '\n'.
At this point you have two choices: either write a row at a time, or don't write a character array. The latter is the easier.
t = cellstr(txt);
fprintf('%s\n', t{:});

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!