複数のテキストファイルを読み込みと変数の作成

10 views (last 30 days)
Kouki
Kouki on 29 Jan 2020
Commented: Kouki on 29 Jan 2020
data_num=100;
file_num=3;
X=zeros(data_num,1);
Y=zeros(data_num,1);
Z=zeros(data_num,1);
T=zeros(data_num,1);
X_mode=zeros(file_num,1);
Y_mode=zeros(file_num,1);
Z_mode=zeros(file_num,1);
fid=zeros(file_num,1);
for i=1:file_num
fid=fopen('point1_%d.txt','r',i);
for j=1:data_num
tline = fgetl(fid);
if (isempty(tline)==1)
break
elseif (tline==-1)
break
else
%XDATA%
x_start=find(tline=='=');
x_end=find(tline=='[');
val_x= tline(x_start+1:x_end-1);
val_x=str2num(val_x);
%YDATA%
tline = fgetl(fid);
y_start=find(tline=='=');
y_end=find(tline=='[');
val_y= tline(y_start+1:y_end-1);
val_y=str2num(val_y);
%ZDATA%
tline = fgetl(fid);
z_start=find(tline=='=');
z_end=find(tline=='[');
val_z= tline(z_start+1:z_end-1);
val_z=str2num(val_z);
%TIME%
tline = fgetl(fid);
%hour
h_start=find(tline=='>');
val_h= tline(h_start-14:h_start-13);
val_h=str2num(val_h);
%min
m_start=find(tline=='>');
val_m= tline(m_start-11:m_start-10);
val_m=str2num(val_m);
%second
s_start=find(tline=='>');
val_s= tline(s_start-8:s_start-7);
val_s=str2num(val_s);
%TIME
val_t=val_h*3600+val_m*60+val_s;
%recording place
X(k)=val_x;
Y(k)=val_y;
Z(k)=val_z;
T(k)=val_t;
end
X_mode(i)=mode(X);
Y_mode(i)=mode(Y);
Z_mode(i)=mode(Z);
end
end
複数のテキストファイル(point1_1.txt,point1_2.txt,point1_3.txt)を1つ読むごとに変数を作成し,この変数の最頻値を求めたいです.
fid=fopen('point1_%d.txt','r',i);の箇所でiがmachinefmtと認識されているのか,マシン形式が無効です。と表示されてしまいます.

Accepted Answer

Takumi
Takumi on 29 Jan 2020
fid=fopen('point1_%d.txt','r',i);
のところを
fname=sprintf('point1_%d.txt',i);
fid=fopen(fname,'r');
とするのはどうですか?
  1 Comment
Kouki
Kouki on 29 Jan 2020
うまくいきました!
ありがとうございます.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!