how to read files with different name using fid

3 vues (au cours des 30 derniers jours)
zein
zein le 22 Oct 2021
Commenté : Stephen23 le 22 Oct 2021
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

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Large Files and Big Data dans Help Center et File Exchange

Tags

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by