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
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개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by