Effacer les filtres
Effacer les filtres

textscan for string with white spaces

2 vues (au cours des 30 derniers jours)
Eric
Eric le 12 Juil 2013
I use textscan to read in data from a file with the format as follows:
1790 2700 red blue white
2783 3185
4835 4849
4854 4865 blue white
fid = fopen(filename);
data = textscan(fid, '%d%d%s');
fclose(fid);
And would like my variable data consists of 3 cells, the first two cells that are integers and the last cell contains empty string or string with white spaces. I failed to produce the output after many attempts. Is there a better way to do that? Thanks.

Réponses (1)

David Sanchez
David Sanchez le 12 Juil 2013
Try horzcat to concatenate strings with empty spaces. Example 1:
str_1 = 'test';
str_2 = ' '; % empty space
str_3 = 'something';
new_str = horzcat(str_1,str_2,str_3)
Example 2:
str_1 = 'test';
str_2 = ' something'; % empty space at the beginning
new_str = horzcat(str_1,str_2)
  1 commentaire
Eric
Eric le 12 Juil 2013
Thanks for your prompt reply, David. Maybe I din't make it clear. I wanted to read from a file and produce 3 output cells. The problem is textscan's '%s' will stop when it comes to a white space. Unless I use multiple '%s', '%s%s%s' for that purpose. However I could not guarantee there are at most 3 words, it could be more. Maybe there is a more elegant way of doing that.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Export 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