fname = Sprintf of the file name

86 views (last 30 days)
Souvik Ghosh
Souvik Ghosh on 28 Sep 2015
Edited: Stephen on 18 Apr 2016
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.

Answers (2)

Stephen
Stephen on 28 Sep 2015
Edited: Stephen on 18 Apr 2016
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 Comments
Stephen
Stephen on 6 Oct 2015
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.

Sign in to comment.


Marius Mueller
Marius Mueller on 28 Sep 2015
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'.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!