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 CenterFile Exchange에서 Large 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