fscanfを用いた​行列ファイルデータの​読み取りについて

14 views (last 30 days)
Yusaku Ando
Yusaku Ando on 6 Oct 2017
Edited: Jiro Doke on 10 Oct 2017
行列の配列が異なる複数のtxtファイルを下のようにリストにまとめ、ループ中でfscanfによって行列を読み取ると、x行y列のデータがx×y行1列で表示されてしまいます。x=fscanf(infile,'%f'、[x y]);とすれば表示できると思いますが他のデータと配列が異なるので自動的に元の配列で表示させたいです。どのように訂正すれ可能になるでしょうか。
LIST=dir('*.txt');
file=fopen('fname.txt','wt');
n=length(LIST);
  for i=1:n
  out=LIST(i,1).name;
fprintf(file,'%s\n',out);
end
fclose(file);
fid=fopen('fname.txt');
data=textscan(fid,'%s');
infnames=data{1,1};
n=length(infnames);
for i=1:n
fname=infnames(i,1);
fname=char(fname);
[pathstr, name, ext] = fileparts(fname);
infile=fopen(fname);
s=fscanf(infile,'%f');
%省略
end
  

Accepted Answer

Jiro Doke
Jiro Doke on 10 Oct 2017
Edited: Jiro Doke on 10 Oct 2017
データ(数値)だけの区切りファイルなのでしたら fopen fscanf を使わずに dlmread などで読み込めます。例えば、このようなテキスト ファイルがあるとします。
1 2 3 4
5 6 7 8
fopen の代わりに
>> s = dlmread('data.txt')
s =
1 2 3 4
5 6 7 8

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!