Problem with processing files in a for loop
Show older comments
My problem is that when I use the script below, it works, but when I put it into loop, it crashes. Why is it so?
names = dir('C:\Users\User\Documents\OZEApp\tests\BufforTest*.txt');
names = {names.name};
fileName = names{4};
path = strcat('C:\Users\User\Documents\OZEApp\tests\', fileName);
pathMatlab = strcat('C:\Users\User\Documents\OZEApp\testMatlab\', fileName);
charPath = convertStringsToChars(path);
charPathMatlab = convertStringsToChars(pathMatlab);
Data = fileread(charPath);
Data = strrep(Data, ',', '.');
New = fopen(charPathMatlab, 'w');
fwrite(New, Data, 'char');
fclose(New);
fid=fopen(charPathMatlab);
This one doesn't work:
names = dir('C:\Users\User\Documents\OZEApp\tests\BufforTest*.txt');
names = {names.name};
for fileName = names'
path = strcat('C:\Users\User\Documents\OZEApp\tests\', fileName);
pathMatlab = strcat('C:\Users\User\Documents\OZEApp\testMatlab\', fileName);
charPath = convertStringsToChars(path);
charPathMatlab = convertStringsToChars(pathMatlab);
Data = fileread(charPath);
Data = strrep(Data, ',', '.');
New = fopen(charPathMatlab, 'w');
fwrite(New, Data, 'char');
fclose(New);
fid=fopen(charPathMatlab);
end
Accepted Answer
More Answers (1)
Walter Roberson
on 25 Dec 2017
names = dir('C:\Users\User\Documents\OZEApp\tests\BufforTest*.txt');
The above gives names as a structure array in column form
names = {names.name};
The above uses structure expansion, as if you had written
{names(1).name, names(2).name, names(3).name... }
so afterwards names will be a cell array in row form.
for fileName = names'
The names' will convert the cell array in row form into a cell array in column form. So now you are doing
for fileName = Cell_Array_in_Column_Form
When you use for, the variable is sequentially assigned each column of the values on the right hand side. Since you have now have exactly one column, the for loop is going to iterate once, and the variable fileName is going to be assigned the entire column vector cell array. Your code does not expect that.
If you had done
for fileName = names
instead then with names being a row vector, fileName would have been assigned one entry at a time.
Note that with the right hand side being a cell array, the columns are cells, not the contents of cells. It so happens that you only use the variable within the context of strcat() which knows to remove the cell surrounding the character vector.
Your code is doing a number of useless transformations between strings and cell arrays of character vectors.
projectdir_in = 'C:\Users\User\Documents\OZEApp\tests';
projectdir_out = 'C:\Users\User\Documents\OZEApp\testMatlab';
dinfo = dir( fullfile(projectdir_in, 'BufforTest*.txt') );
names = fullfile(projectdir_in, {dinfo.name});
for K = 1 : length(names)
charPath = names{K};
[~, basename, ext] = fileparts(charPath);
pathMatlab = fullfile(projectdir_out, [basename ext]);
Data = fileread(charPath);
Data = strrep(Data, ',', '.');
New = fopen(charPathMatlab, 'w');
fwrite(New, Data);
fclose(New);
end
2 Comments
Waldemar Kolodziejczyk
on 25 Dec 2017
Walter Roberson
on 26 Dec 2017
New = fopen(pathMatlab, 'w');
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!