How to convert character matrix into numeric matrix ? Can you share code for that?
Afficher commentaires plus anciens
I have created a matrix that accepts the text file called pub.txt and content in that file is Hello word alone..and displayed it .
str=fileread('pub.txt')
str =
Hello Bava
Hi Hi
Hi Hi
Now, I want to convert this character matrix into numeric matrix ? Plz help
14 commentaires
Chafik Zerrouki
le 27 Nov 2017
Did you try with str2num() ?
It will Convert character array to numeric array.
bavani marimuthu
le 27 Nov 2017
bavani marimuthu
le 27 Nov 2017
Apparently it is time for an important lesson every programmer must learn at some point: RT(F)M (read the manual). There are some examples you can find in the documentation for str2num. (If you're operating on scalars, use str2double instead. Use %#ok to suppress the m-lint warning.)
PS array and matrix are more or less interchangeable terms in Matlab
Adam
le 27 Nov 2017
Technically a matrix is 2d and an array is nd in Matlab, but array includes matrix.
What numeric output are you hoping to get out of
str =
Hello Bava
Hi Hi
Hi Hi
Are you looking at getting the codepoint of the characters or something else?
bavani marimuthu
le 27 Nov 2017
Stephen23
le 27 Nov 2017
@bavani marimuthu: Guillaume asked you a question. Here it is again:
"What numeric output are you hoping to get..?"
Please show us what numeric output you expect to get from those characters.
bavani marimuthu
le 27 Nov 2017
Rik
le 27 Nov 2017
I can't see a way the string below will result in [100 101 0 0 0 0], so you'll have to explain that.
str = ['Hello Bava';'Hi Hi ';'Hi Hi '];
If your strings contain only numeric values in a char representation (like str='100 101 0 0 0 0';), then using str2num will do the trick.
Stephen23
le 27 Nov 2017
"If your strings contain only numeric values in a char representation (like str='100 101 0 0 0 0';), then using str2num will do the trick."
Although using sscanf would be faster and more secure:
>> str = '100 101 0 0 0 0';
>> sscanf(str,'%f')
ans =
100
101
0
0
0
0
bavani marimuthu
le 27 Nov 2017
bavani marimuthu
le 28 Nov 2017
Stephen23
le 28 Nov 2017
double('Hello')
will convert the char matrix into a numeric matrix.
Réponses (0)
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!