필터 지우기
필터 지우기

Why does this code generate an error?

조회 수: 2 (최근 30일)
Chaitanya Bade
Chaitanya Bade 2017년 4월 5일
댓글: the cyclist 2017년 4월 6일
Hi all,
The below code is generating an error :
The code used here is:
%BATCH MODE
files = dir('C:\Users\Deakin Uni\Documents\MATLAB\a');
fNames = {files.name};
%for each file
for k= 1: length(fNames)
% extract file name
fName = ['C:\Users\Deakin Uni\Documents\MATLAB\MAX', fNames{k}];
[XDATA, TIMESTEPS] = importfile(fName);
a1 = XDATA - mean (XDATA);
plot(TIMESTEPS, a1);
[peakvals,peaklocs] = findpeaks(a1, 'minpeakdistance',50, 'minpeakheight', 0.00001);
peaktime = TIMESTEPS(peaklocs);
createfigure4(TIMESTEPS, a1, peaktime, peakvals)
saveas(gcf, 'chaitu.fig')
end
The error here is: Error using xlsread (line 132) XLSREAD unable to open file 'fName'. File 'fName' not found.
Error in importfile (line 36) data = xlsread(workbookFile, sheetName, sprintf('A%d:B%d',startRow(1),endRow(1)));
Thanks in advance
  댓글 수: 2
Adam
Adam 2017년 4월 5일
What is in fNames when you step through the code in the debugger?
Chaitanya Bade
Chaitanya Bade 2017년 4월 5일
편집: the cyclist 2017년 4월 5일
Sorry mate, just realized there were some issues with my directory folder. Have made changes. It's working now. However, it is not running the loop. Just generating solution for the first file and that's it. Wondering if you can help. Thanks!
%BATCH MODE
files = dir('C:\Users\Deakin Uni\Documents\MATLAB\a\*CA90-Wb0R0-00015MAX-X*');
fNames = {files.name};
%for each file
for k= 1: length(fNames)
% extract file name
fName = ['C:\Users\Deakin Uni\Documents\MATLAB\a\', fNames{k}];
[XDATA, TIMESTEPS] = importfile(fName);
a1 = XDATA - mean (XDATA);
plot(TIMESTEPS, a1);
[peakvals,peaklocs] = findpeaks(a1, 'minpeakdistance',50, 'minpeakheight', 0.00001);
peaktime = TIMESTEPS(peaklocs);
createfigure4(TIMESTEPS, a1, peaktime, peakvals)
saveas(gcf, 'chaitu.fig')
end

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

채택된 답변

the cyclist
the cyclist 2017년 4월 5일
Presumably, the cause is that for some value of k,
['C:\Users\Deakin Uni\Documents\MATLAB\MAX', fNames{k}]
does not resolve to a string that corresponds to a file name in that directory.
I suggest you display the name of each file as you try to access it, and that should lead you to the specific file name that is problematic.
  댓글 수: 6
Chaitanya Bade
Chaitanya Bade 2017년 4월 6일
편집: Chaitanya Bade 2017년 4월 6일
@the cyclist: if i put disp(k), it just shows the first file. I wanted to run a loop among all the files within the directory. And it does not show any error. I have got the solution for the first file. But not for the other two files. And the value of length(fNames) is 3.
the cyclist
the cyclist 2017년 4월 6일
Nothing stands out to me (after admittedly not very intense scrutiny). Are you familiar with the ability to enter debug mode? I suggest you use that to step through your program line-by-line from k==1, and see if the values of the variables are what you expect them to be. This should give a clue about what went wrong.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by