how to read files with different name using fid

4 ビュー (過去 30 日間)
zein
zein 2021 年 10 月 22 日
コメント済み: Stephen23 2021 年 10 月 22 日
I am trying to make averaging for data saved in files named:pfoil_1_var_1_562474.raw, pfoil_1_var_1_562476.raw .......pfoil_1_var_1_562494.raw.
I have written the following lines but an error was generated
clc
clear
N = 6710*6; % grid size (number of points in raw file)
fid = fopen('pfoil_1_var_1_562474.raw','r'); % rb = read binary
data = fread(fid,N,'single');
fclose(fid);
start=8;
data1=data(start:length(data));
p=data1;
step=2
a=562476;
b=562494;
for i=a:step:b
fid = fopen('pfoil_1_var_1_',sprintf('%d',i),'.raw','r'); % rb = read binary
data = fread(fid,N,'single');
fclose(fid);
start=8;
data1=data(start:length(data));
p2=data1;
p=p+p2;
end
the error generated is
Error using fopen
Invalid permission.
Error in readsubspace_p (line 14)
fid = fopen('pfoil_1_var_1_',sprintf('%d',i),'.raw','r'); % rb = read binary
How to write this line correclty so the name of the file to be read change within the for loop
fid = fopen('pfoil_1_var_1_',sprintf('%d',i),'.raw','r'); % rb = read binary

採用された回答

Stephen23
Stephen23 2021 年 10 月 22 日
fnm = sprintf('pfoil_1_var_1_%d.raw',i);
fid = fopen(fnm,'r');
  2 件のコメント
zein
zein 2021 年 10 月 22 日
I used the followning lines and it worked properly
filename=['pfoil_1_var_1_',num2str(i),'.raw'];
fid = fopen(filename,'rb'); % rb = read binary
Stephen23
Stephen23 2021 年 10 月 22 日
I recommend using SPRINTF rather than NUM2STR and string concatenation: it is fewer commands, more efficient, and gives you more control.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by