Borrar filtros
Borrar filtros

how to use the str2num for the whole array at once

39 visualizaciones (últimos 30 días)
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD el 1 de Nov. de 2017
Editada: per isakson el 1 de Nov. de 2017
Dear all, when I read a text file and import it as an array A, sometimes I use the function str2num to return the imported vectors into a numerical form, but I need to do it for each vector alone. is there anyway please to reshape the whole array A into a numeric matrix at one time? like can i use the str2num to the whole matrix A and call it B for example?
thank you
  13 comentarios
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD el 1 de Nov. de 2017
i have older than what Mr Walter said, i am sorry
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD el 1 de Nov. de 2017
I have an 8 giga ram

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 1 de Nov. de 2017
Editada: per isakson el 1 de Nov. de 2017
"I have older than what Mr Walter said" Thus the old way
Read the file and convert the content to an old time string array, str
fid = fopen( 'NewTextDocument.txt' );
cac = textscan( fid, '%s', 'Delimiter', '\n' );
fclose( fid );
str = char( cac{1} );
str( str==' ' ) = '0';
Inspect str
>> whos str
Name Size Bytes Class Attributes
str 50x105 10500 char
and
>> str(1,:)
ans = W39000779310707010002000001802010029008000000000000000000000000000000000000000000000000000000000000000000
Now you have a string array with the width of the longest row. The function, char, padded with space, which I replaced by the character '0'. (I padded one line of the text file with "0" up till the column 105.)
Now convert columns of the character array to numerical column vectors
Vehicle_Class = str2num( str(:,20:21) );
D_Axle_Weight = str2num( str(:,49:51) );
and inspect the result
>> whos Vehicle_Class D_Axle_Weight
Name Size Bytes Class Attributes
D_Axle_Weight 50x1 400 double
Vehicle_Class 50x1 400 double
With these pieces of code I think you can make your script work.
  2 comentarios
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD el 1 de Nov. de 2017
Thank you very much Mr Per, this is very very helpful and great. I appreciate your help Sir.
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD el 1 de Nov. de 2017
really thank you for your time and effort, this is amazing Sir

Iniciar sesión para comentar.

Más respuestas (2)

Nicolas Schmit
Nicolas Schmit el 1 de Nov. de 2017
How about this?
A = {'1', '2', '3'}
B = cellfun(@str2num, A)
  2 comentarios
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD el 1 de Nov. de 2017
This looks awsome man, I will use it and let you know what happens. thank you very much
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD el 1 de Nov. de 2017
Hi Nicolas, I tried it and gave me this message: Input #2 expected to be a cell array, was char instead.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 1 de Nov. de 2017
You have fixed width fields. You should see https://www.mathworks.com/help/matlab/ref/fixedwidthimportoptions.html if you have R2017a or later.
  1 comentario
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD el 1 de Nov. de 2017
Yes I am reading through this thank you, I think the problem is in my files, because they are not rectangular and each row does not have the same dimensions of the rest. I could open it, read it and divide it but the thing is it is not going beyond character 93, while i need it to reach 105

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by