MATLAB Answers

0

How can I save a word e.g. 'apple' in a text file, without matlab separating every letter with a comma?

Asked by Lala0099 on 14 Apr 2019
Latest activity Answered by dpb
on 14 Apr 2019
Accepted Answer by dpb
So i am trying to save a text, into a txt file. e.g. i like apples, not bananas.
BUt the txt file has every single letter separated by commas. How can I join them togetehr when read by another function ? or not save it like that, but as a text?

  1 Comments

Sign in to comment.

1 Answers

Answer by dpb
on 14 Apr 2019
 Accepted Answer

You undoubtedly use csvwrite or similar to write a character array -- internally a char() array is just that--an array of numbers that happen to be have the display characteristics of the character represented instead of the number underlying it--but csvwrite doesn't know that; it just writes the elements of the array as individual numbers. When retrieved, those numeric values are still those of the character so they are displayed correctly, but now they are individual array elements, not the array...it's why you have to use 2D addressing on a char() string to get the whole string, not just the first character.
>> csvwrite('apple.txt','apple') % mimic what you (apparently) did...
>> type apple.txt % symptom you saw...
a,p,p,l,e
fid=fopen('apple.txt','w'); % open a file handle with write permission...
fprintf(fid,'%s\n','apple'); % write a record w/ \n
fid=fclose(fid); % close the file
>> type apple.txt % now see what it contains
apple
>>
It's somewhat of a pain -- the Matlab i/o functions have progressed remarkably for input of text files of various forms; unfortunately, the output side is "not so much" as yet.
There are two higher-level routines writetable and xlswrite that can do some nice things, but they also have their limitations as to what they can do...

  0 Comments

Sign in to comment.



Translated by