Problem with processing files in a for loop

조회 수: 8 (최근 30일)
Waldemar Kolodziejczyk
Waldemar Kolodziejczyk 2017년 12월 25일
편집: dpb 2017년 12월 26일
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

채택된 답변

dpb
dpb 2017년 12월 25일
편집: dpb 2017년 12월 26일
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
Waldemar Kolodziejczyk
Waldemar Kolodziejczyk 2017년 12월 25일
Still I have same error: Error using fopen. First input must be a file name or a file identifier.Error in Bufforplot (line 7) New = fopen(charPathMatlab, 'w');
dpb
dpb 2017년 12월 26일
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...

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
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
Waldemar Kolodziejczyk 2017년 12월 25일
Thats strange because when I implemented your code, It throws the same error...
Error using fopen First input must be a file name or a file identifier.
Error in Bufforplot (line 11) New = fopen(charPathMatlab, 'w');
Walter Roberson
Walter Roberson 2017년 12월 26일
New = fopen(pathMatlab, 'w');

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by