fname = Sprintf of the file name

27 次查看(过去 30 天)
Souvik Ghosh
Souvik Ghosh 2015-9-28
编辑: Stephen23 2024-10-1
I have written a matlab code to read and average certain data in number of files in a folder and write the averaged value back as a file in the same folder. But I am getting some issues in the code.
clear alltopline
close all
clc
n_in = 2;
n_out = 15362;
N_input = 7;
N_time = 20;
for j=1:N_time
fname = sprintf('vel_xz00%01.f0000',j);
fdata = fopen(fname);
for i=1:2
topline = fgetl(fdata);
end
N_tot = (n_out - n_in)*N_input;
Data_tmp = fscanf(fdata,'%f',N_tot);
fclose(fdata);
for i=1:N_input
Time(j).Data_sim(:,i) = Data_tmp(i:7:N_tot);
end
end
My files in the folder have the filename as vel_xz0010000 till vel_xz0200000. The code can read the values of the datas from files vel_xz0010000 till vel_xz0090000 but after that cannot read the values from vel_xz0100000 til vel_xz0200000. It gives the following error:
Error using fgets Invalid file identifier. Use fopen to generate a valid file identifier.
Error in fgetl (line 33) [tline,lt] = fgets(fid);
Error in Flow_field_postprocessing (line 16) topline = fgetl(fdata);
Also these files have datas in 15360 X 7 matrix. I want to get the average values of (1,1)element to (15360,7)element of all the files from vel_xz0010000 to vel_xz0200000 and store them as a new vel_xz file with 15360 X 7 elements. Like average of all the (1,1) elements of the 20 files, average of all the (1,2) elements of the 20 files and like so on.

回答(2 个)

Stephen23
Stephen23 2015-9-28
编辑:Stephen23 2016-4-18
The problem is how you are generating the names does not take into account the zeros on the left and right of the non-zero digit. Here are two solutions:
Use dir
It would be easier to use dir and read the filenames from the directory:
S = dir('vel_xz*.f0000');
N = {S.name};
for k = 1:numel(N)
fid = fopen(N{k});
... etc
end
Use fixed length field with leading zeros
If you particularly want to use sprint and generate the filenames yourself, then you need to consider the zeros in the names. In your examples the total number of digits is seven, with leading zeros. This can be created with the correct sprint format string:
for k = 1:20
sprintf('vel_xz%03d0000',k)
end
displays these strings in the command window:
ans = vel_xz0010000
ans = vel_xz0020000
ans = vel_xz0030000
ans = vel_xz0040000
ans = vel_xz0050000
ans = vel_xz0060000
ans = vel_xz0070000
ans = vel_xz0080000
ans = vel_xz0090000
ans = vel_xz0100000
ans = vel_xz0110000
ans = vel_xz0120000
ans = vel_xz0130000
ans = vel_xz0140000
ans = vel_xz0150000
ans = vel_xz0160000
ans = vel_xz0170000
ans = vel_xz0180000
ans = vel_xz0190000
ans = vel_xz0200000
Averaging matrices
Do not read each file line-by-line, this will be terribly inefficient and slow code. Simply read all of the files into one 3D array, and then the average can be performed with one simple command:
S = dir('vel_xz*.f0000');
N = {S.name};
for k = numel(N):-1:1 % iterate backwards for speed
M(:,:,k) = importdata(N{k});
end
X = mean(M,3); % take average along third dimension
csvwrite('vel_vx_Average.f0000',M)
  6 个评论
Stephen23
Stephen23 2015-10-5
编辑:Stephen23 2024-10-1
You are still using the same format string, even after I explained how you can change it.
You should experiment with this:
for k = 1:100
sprintf('vel_xz000%03d0',k)
end
Compare the format string from my original answer, and the new one:
'vel_xz%03d0000'
'vel_xz000%03d0'
Perhaps you could read the sprint documentation and try experimenting a little bit with that format string. Your computer will not catch fire if you make a mistake, I promise you.
Stephen23
Stephen23 2015-10-6
编辑:Stephen23 2015-10-6
NOTE: The OP deleted all of their comments, but I will leave my responses anyway.
NOTE to OP: Please do not delete your comments and questions. I am not your personal MATLAB tutor, and I am not here to provide you and you alone with solutions to your problems. This is a public forum and our discussion is here to help anyone else who might be interested in the same topic. Because you deleted your comments my responses no longer make much sense, and will not be useful to others reading this forum.

请先登录,再进行评论。


Marius Mueller
Marius Mueller 2015-9-28
This might be due to your var fname:
fname = sprintf('vel_xz00%01.f0000',j);
after the xz you have too many zeros to create a file named 'xz0100000'. You want to change this to ever create a fname 'xz0100000'.

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by