Problem with processing files in a for loop
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
Walter Roberson
2017년 12월 25일
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
Waldemar Kolodziejczyk
2017년 12월 25일
Walter Roberson
2017년 12월 26일
New = fopen(pathMatlab, 'w');
카테고리
도움말 센터 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!