How to convert character matrix into numeric matrix ? Can you share code for that?

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

Did you try with str2num() ?
It will Convert character array to numeric array.
can you explain me with the example?
I want it for character matrix into numeric matrix , Not for Array?
Rik
Rik le 27 Nov 2017
Modifié(e) : Rik 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
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?
I have created a matrix that accepts the text file called pub.txt code: str=fileread('pub.txt')
Now, I want to convert this character matrix into numeric matrix ? Plz help
@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.
The output I except is this... 100 101 0 0 0 0
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.
"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
If my .txt file contains the character called 'Hello' .. Then Whether is there any possibilities to convert it into numeric matrix from character matrix which contains the character in it..
double('Hello')
will convert the char matrix into a numeric matrix.

Connectez-vous pour commenter.

Réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by