Effacer les filtres
Effacer les filtres

Why does MATLAB do this, wrt fscanf from txt document?

2 vues (au cours des 30 derniers jours)
Samyukta Ramnath
Samyukta Ramnath le 4 Juin 2013
I imported a .txt document using fopen and fscanf, and used the following two lines:
fid = fopen('History2.txt');
words = fscanf(fid, '%s');
words
if true
% code
end
OUTPUT : words =
Hello,thistellsusthehistoryofvariousrulersintheCholaperiod.Historyisimportantbecauseittellsuswhatnottodointhefuture.That'swhyhistoryisthebestthingtodo,whenhistoryisthere,wefeelsafeandsound.
When I did this instead, fid = fopen('History2.txt'); words = fscanf(fid, '%c'); words if true % code end
OUTPUT : words =
Hello, this tells us the history of various rulers in the Chola period. History is important because it tells us what not to do in the future. That's why history is the best thing to do, when history is there, we feel safe and sound.
Any idea why just changing it from fscanf using %s to %c retained the whitespaces?

Réponse acceptée

Jan
Jan le 4 Juin 2013
The '%c' format specifier let FSCANF import all characters, even spaces, while '%s' imports words, which are separated by white-space characters. So you observe the expected behavior.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by