how to read files with different name using fid

3 visualizaciones (últimos 30 días)
zein
zein el 22 de Oct. de 2021
Comentada: Stephen23 el 22 de Oct. de 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

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by