error using horzcat for multiple files
显示 更早的评论
hello everyone, i wrote this code to read text files with extension YO7, each file represents a month in the year, when i run it for 2 cards (2 months) it works, but when i add the third card or month it gives error, any ideas what to do here please?
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(isnan(A)))=0;
[rows,columns]=size(A);
if columns~=105
ArrayTemp=zeros(rows,105);
ArrayTemp(1:rows,1:columns)=A;
A=ArrayTemp;
A=char(A);
A(isspace(A)) = '0';
end
x1=filename;
xtr=strcat('C:\Users\maa285\Desktop\New folder (2)',x1);
fid = fopen( xtr, 'wt' );
StationID = A(:,4:9);
Year_of_Data = A(:,12:13);
Month_of_Data = A(:,14:15);
Vehicle_Class = A(:,20:21);
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);
%This is to convert the string to numbers so it can show the output:
fprintf(fid, '%s %s %s %s %s',StationID, Year_of_Data, Month_of_Data, Vehicle_Class, Total_Weight_of_vehicle, Number_of_axles, A_Axle_Weight, A_B_Axle_spacing, B_Axle_Weight, B_C_Axle_spacing, C_Axle_Weight, C_D_Axle_spacing, D_Axle_Weight, D_E_Axle_spacing, E_Axle_Weight);
Ans_3{K}=str2num(StationID);
Ans_6{K}=str2num(Year_of_Data);
Ans_7{K}=str2num(Month_of_Data);
Ans_10{K}=str2num(Vehicle_Class);
Ans_12{K}=str2num(Total_Weight_of_vehicle);
Ans_13{K}=str2num(Number_of_axles);
Ans_14{K}=str2num(A_Axle_Weight);
Ans_15{K}=str2num(A_B_Axle_spacing);
Ans_16{K}=str2num(B_Axle_Weight);
Ans_17{K}=str2num(B_C_Axle_spacing);
Ans_18{K}=str2num(C_Axle_Weight);
Ans_19{K}=str2num(C_D_Axle_spacing);
Ans_20{K}=str2num(D_Axle_Weight);
Ans_21{K}=str2num(D_E_Axle_spacing);
Ans_22{K}=str2num(E_Axle_Weight);
%%to return the separated vectors into one new matrix with usable data:
all{K} =[Ans_3{K},Ans_6{K},Ans_7{K},Ans_10{K},Ans_12{K},Ans_13{K},Ans_14{K},Ans_15{K},Ans_16{K},Ans_17{K},Ans_18{K},Ans_19{K},Ans_20{K},Ans_21{K},Ans_22{K}];
end
6 个评论
Walter Roberson
2017-12-6
Which line is the error occurring on?
Note: your code line
fprintf(fid, '%s %s %s %s %s',StationID, Year_of_Data, Month_of_Data, Vehicle_Class, Total_Weight_of_vehicle, Number_of_axles, A_Axle_Weight, A_B_Axle_spacing, B_Axle_Weight, B_C_Axle_spacing, C_Axle_Weight, C_D_Axle_spacing, D_Axle_Weight, D_E_Axle_spacing, E_Axle_Weight);
is suspicious. Each of those variables looks to be a character array with multiple rows. fprintf() in MATLAB "uses up" all of the first column of the first variable, then all of the second column of the first variable, and so on, before it starts using the second variable at all, so you would get garbled output unless I have missed some reason why A really only has one row even though you consistently refer to it as if it has multiple rows.
MAHMOUD ALZIOUD
2017-12-6
Walter Roberson
2017-12-6
If A has multiple rows then that fprintf() is definitely wrong. You need
temp = [cellstr(StationID), cellstr(Year_of_Data), cellstr(Month_of_Data), cellstr(Vehicle_Class), cellstr(Total_Weight_of_vehicle), cellstr(Number_of_axles), cellstr(A_Axle_Weight), cellstr(A_B_Axle_spacing), cellstr(B_Axle_Weight), cellstr(B_C_Axle_spacing), cellstr(C_Axle_Weight), cellstr(C_D_Axle_spacing), celstr(D_Axle_Weight), cellstr(D_E_Axle_spacing), cellstr(E_Axle_Weight)] .';
printf(fid, '%s %s %s %s %s\n', temp{:});
Walter Roberson
2017-12-6
I suggest you command
dbstop if error
and run the program, and when it stops, examine the size of each of those Ans_ cell arrays.
MAHMOUD ALZIOUD
2017-12-6
MAHMOUD ALZIOUD
2017-12-6
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!