how to use the str2num for the whole array at once

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 Comments

  • "array A" is that a cell array?
  • Could you provide a tiny example?
  • Why import a text file with numerical data into character data and convert to numerical in a second step?
I import data from a text file and gives me an array of 300000x105 dimension. i use this code
directory=dir('*.Y07');
count=0;
for K = 1 : length(directory)
filename = directory(K).name;
fileID = fopen(filename,'r');
formatSpec = '%s';
A_cell = textscan(fileID,formatSpec);
A=char(A_cell{1,1}{:,:});
A(find(A==nan))=0;
% y=A;
[rows,columns]=size(A);
@MAHMOUD ALZIOUD: why do you use char? What is the file format? Is the file data string, numeric, or mixed?
It would be most useful if you uploaded a sample file for us to work with: add a comment and click the paperclip button. The sample file does not need to be the full file, just enough to be representative of the file format.
Thank you Mr Steph, I import data from a text file that is must be divided into columns using the fixed width inputs. the output data is called A and it is a character of a size 300000x105. I have a problem here that the first line must be 105 characters but in matlab when i import the file it just takes the first 93. can i please pre-determine the matrix dimension before i import?
@MAHMOUD ALZIOUD: please make a new comment and upload a sample file by clicking the paperclip button.
We cannot run code using a screenshot, and no one is going to copy all that data from your screenshot. Screenshots are useless for doing anything with.
I am trying to do so but the file is too large, the code i am using to import and divide data is this, note that it must be 105 characters in the columns but it just imports 93! how can i send you the text file please?
clear;
clc;
directory=dir('*.Y07');
count=0;
for K = 1 : length(directory)
filename = directory(K).name;
fileID = fopen(filename,'r');
formatSpec = '%s';
A_cell = textscan(fileID,formatSpec);
A=char(A_cell{1,1}{:,:});
A(find(A==nan))=0;
A(isspace(A)) = '0';
[rows,columns]=size(A);
x1=filename;
xtr=strcat('C:\Users\maa285\Desktop\New folder (2)\',x1);
fid = fopen( xtr, 'wt' );
Record_Type = A(:,1:1);
FibsCode = A(:,2:3);
StationID = A(:,4:9);
Direction_Of_Travel = A(:,10:10);
Lane_Of_Travel = A(:,11:11);
Year_of_Data = A(:,12:13);
Month_of_Data = A(:,14:15);
Day_of_Data = A(:,16:17);
Hour_of_Data = A(:,18:19);
Vehicle_Class = A(:,20:21);
Open = A(:,22:24);
Total_Weight_of_vehicle = A(:,25:28);
Number_of_axles = A(:,29:30);
A_Axle_Weight = A(:,31:33);
A_B_Axle_spacing = A(:,34:36);
B_Axle_Weight = A(:,37:39);
B_C_Axle_spacing = A(:,40:42);
C_Axle_Weight = A(:,43:45);
C_D_Axle_spacing = A(:,46:48);
D_Axle_Weight = A(:,49:51);
D_E_Axle_spacing = A(:,52:54);
E_Axle_Weight = A(:,55:57);
E_F_Axle_spacing = A(:,58:60);
F_Axle_Weight = A(:,61:63);
F_G_Axle_spacing= A(:,64:66);
G_Axle_Weight = A(:,67:69);
G_H_Axle_spacing = A(:,70:72);
H_Axle_Weight = A(:,73:75);
H_I_Axle_spacing = A(:,76:78);
I_Axle_Weight= A(:,79:81);
I_J_Axle_spacing = A(:,82:84);
J_Axle_Weight = A(:,85:87);
J_K_Axle_spacing= A(:,88:90);
K_Axle_Weight = A(:,91:93);
K_L_Axle_spacing = A(:,94:96);
L_Axle_Weight = A(:,97:99);
L_M_Axle_spacing = A(:,100:102);
M_Axle_Weight = A(:,103:105);
end
"note that it must be 105 characters in the columns"
  • The longest row of NewTextDocument.txt is 57 characters.
  • Does that mean that the rows shall be padded with space?
  • "Open = A(:,22:24)" those columns contains space
  • "xxxxx_spacing" What kind of variables are those that contain "spacing" in their names?
@MAHMOUD ALZIOUD: can you tell us something about the file format? Note that the answer to your original question would be to simply load all of that numeric data as numeric data. There is absolutely no point in loading it as strings and then converting to numeric.
FOR Mr Per Note: I have 12 cards (or files), each card covers a month. but they do not have the same length in columns, some rows may or will have less than 105 characters, but the max length should be 105 as divided in the code above. the open field yes it has spaces and i need to fill the whole blanks with zeros. the spacing means the distance in inches between axles, they are numeric numbers. for example, if i have a row that is 66 character in length, i must fill the rest with zeros in it until i reach 105 characters for each row.
****************
Mr Steph, the file extension is called Y07 and it is ascii file i believe with text format. what is the best way to handle this file?
  • With releases older than R2017a (see Walters answer) you have to use an approach similar to your code.
  • Which release do you use?
  • The sizes of the intermediate variables are less than 100MB, thus the entire file may be read into memory. What's the size of your RAM?
i have older than what Mr Walter said, i am sorry

Sign in to comment.

 Accepted Answer

"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 Comments

Thank you very much Mr Per, this is very very helpful and great. I appreciate your help Sir.
really thank you for your time and effort, this is amazing Sir

Sign in to comment.

More Answers (2)

How about this?
A = {'1', '2', '3'}
B = cellfun(@str2num, A)

2 Comments

This looks awsome man, I will use it and let you know what happens. thank you very much
Hi Nicolas, I tried it and gave me this message: Input #2 expected to be a cell array, was char instead.

Sign in to comment.

You have fixed width fields. You should see https://www.mathworks.com/help/matlab/ref/fixedwidthimportoptions.html if you have R2017a or later.

1 Comment

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

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!