How can I save a word e.g. 'apple' in a text file, without matlab separating every letter with a comma?
2 次查看(过去 30 天)
显示 更早的评论
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?
采纳的回答
dpb
2019-4-14
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!