MATLAB: Load and write multiple txt-files – Interruption without error message

for loopfprintfloadMATLAB and Simulink Student Suitesavetext file

I load 310 txt-files (each of them has 1000 rows and 31 columns) from a directory with the following code:
function create_paramfile
clear all, close all
% create 310 * 1000 parameter-files
% first step: load all files
pathname = 'C:\Users\Documents\MATLAB\ma\sesxc';
files = dir(fullfile(pathname,'*.txt'));
for k=1:length(files)
param = load(fullfile(pathname, files(k).name),'-ascii');
alb_newsnow=param(:,1);
alb_oldsnow=param(:,2);
alb_decrease=param(:,3);
...
n = length(param); % param has 1000 rows and 31 columns
The load command works fine and all txt-files are listed in the Workspace of the Matlab-Window.
In the next step, (within the function create_paramfile), I try to write alb_newsnow, … to a new structured txt-file. The code therefore is:
my_dir = pwd;
cd(my_dir);
addpath(genpath(my_dir));
cd([ my_dir '/new_xc']);
% delete param.ini files within im my_dir/new_xc
delete('*.ini')
% write all files for new parameter
for i=1:n;
file_name = ['param' num2str(k(:)) num2str(i(:).') '.ini'];
save(file_name);
fid = fopen(file_name,'w');
fprintf(fid,'%s\n','#---');
fprintf(fid,'%s\n','#Parameter File for model');
fprintf(fid,'%s\n','#----');
fprintf(fid,'%s\n','#<ALBEDO>');
fprintf(fid,'%s\t','alb_newsnow');
fprintf(fid,'%g\n',alb_newsnow(i,1));
fprintf(fid,'%s\t','alb_oldsnow');
fprintf(fid,'%g\n',alb_oldsnow(i,1));
fprintf(fid,'%s\t','alb_decrease');
fprintf(fid,'%g\n',alb_decrease(i,1));
end
end
The problem is that only the first of 310 txt-files is processed. That means that I get 1000 files from this file and no output for the second, third, … file. If everything would run correctly, finally, 310000 files would have to be written.
Where could be the problem?

Best Answer

Do not start a function with the darn clear all. This removed all loaded functions from the memory and is a massive waste of time in consequence. It does not even clear the workspace in your case, because the workspace of a function is empty at its beginning.
You want to write 310'000 files?!
Avoid using cd, pwd and relative file names. This is prone to unexpected behavior.
my_dir = pwd;
cd(my_dir);
addpath(genpath(my_dir));
cd([ my_dir '/new_xc']);
This does not seem to be useful. What do you want to achieve here?
What is the prupose of save(file_name)? The created file is deleted in the next line.
Whenever you open a file by fopen care for closing it properly by fclose.
This looks strange:
file_name = ['param' num2str(k(:)) num2str(i(:).') '.ini']
What is the purpose of the (:) behind k and i? Why do you transpose I(:)? Do you mean:
file_name = sprintf('param%d%d.ini', k, i)
? Then the created file names are not unique: k=12 and i=1 creates the same file name "param121.ini". Better:
file_name = sprintf('param%04d_%04d.ini', k, i)
for "param012_001.ini".