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

2 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

dpb
dpb le 14 Avr 2019
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...

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by