Problem with processing files in a for loop
5 views (last 30 days)
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
0 Comments
Accepted Answer
dpb
on 25 Dec 2017
Edited: dpb
on 26 Dec 2017
Don't say what doesn't work or give error message, but the first dereferences a single element of the directory structure while the last simply provides the cell content. Many (most) of the file handling routines haven't been updated to use cellstr so guessing that would be where the problem lies...
Can write a little more simply if just use straight-ahead constructs instead of trying to get fancy--
d=dir('C:\Users\User\Documents\OZEApp\tests\BufforTest*.txt');
for i=1:length(d)
path = fullfile('C:\Users\User\Documents\OZEApp\tests', d(i).name);
pathMatlab = fullfile('C:\Users\User\Documents\OZEApp\testMatlab\', d(i).name);
Data = fileread(charPath);
Data = strrep(Data, ',', '.');
New = fopen(PathMatlab, 'w');
fwrite(New, Data, 'char');
fclose(New);
end
You had an fopen call inside the loop which would also cause issues of opening each file in turn but overwrite the same file handle as were using a single variable fid
2 Comments
dpb
on 26 Dec 2017
Yeah, I didn't get rid of the last fignewton of your code completely... charPathMatlab went away completely; use just pathMatlab; as Walter notes you're messing around way too much between cellstr and char representation of the same thing; as noted just use the char string from the struct directly and then don't need the other...
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
See Also
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!