how to remain the original matrix dimension and remove blank cells

1 次查看(过去 30 天)
Dear All, I imported data from a text file, this text file has 300000 rows and must have 105 columns for example. when i import it as vector matrices, all the vectors must have a length of 300000 as the original matrix A (which is the file). but it does not do that, I found out the reason is maybe because many rows has blank cells, how can i please tell MATLAB to remove the blank cells and put zeros instead so I keep the original dimension? thank you in advance

回答(1 个)

Image Analyst
Image Analyst 2017-10-30
If you've got blank rows at strange places, then you may just have to read in the file line-by-line with fgetl() and check for bad or blank rows. Then, if the row is good, get the numbers out, else skip it. See help for fgetl(,) and sscanf() or textscan().
  2 个评论
MAHMOUD ALZIOUD
MAHMOUD ALZIOUD 2017-10-30
now this is my code to read the file and how to divide the columns based on a fixed width. i do not know why it stops and column 93 (whcih is K_Axle_Weight) when actullay it must continue to 105.
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);
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by