how to read files with different name using fid

3 views (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 22 Oct 2021
fnm = sprintf('pfoil_1_var_1_%d.raw',i);
fid = fopen(fnm,'r');
  2 Comments
zein
zein on 22 Oct 2021
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 on 22 Oct 2021
I recommend using SPRINTF rather than NUM2STR and string concatenation: it is fewer commands, more efficient, and gives you more control.

Sign in to comment.

More Answers (0)

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!