Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
I am recieving the error message below for a for loop I wrote. I believe it because I am attemting to store a vector as a scaler but am not sure. New to MatLab.
조회 수: 1 (최근 30일)
이전 댓글 표시
In an assignment A(:) = B, the number of elements in A
and B must be tbe the same.
Error in Learningp1_T1_dataVeh_10 (line 61)
idx(a) = max(strfind(char(filename{a}),'.'));
=============================================================
%Determine the test schedules names
for a = 1:1:2
idx(a) = max(strfind(char(filename{a}),'.'));
name = char(filename{a});
fname{a} = name(1:idx(a)-1);
end
for a = 1:1:2
idx(a) = max(strfind(char(filename{a}),'.'));
name = char(filename{a});
fname{a} = name(1:idx(a)-1);
end
댓글 수: 0
답변 (2개)
Geoff Hayes
2018년 5월 17일
Chris - I suspect that one of your filenames does not have a period in it...and so the "empty" case is causing it to fail. For example,
filenames = {'test.m', 'test'};
id(1) = max(strfind(filenames{1},'.'));
id(2) = max(strfind(filenames{2},'.'));
If I run the above code, then I get the same error as you. I suppose you could add a check as
filenames = {'test.m', 'test'};
for k=1:length(filenames)
index = max(strfind(filenames{k},'.'));
if isempty(index)
idx(k) = length(filenames{k}) + 1;
else
idx(k) = index;
end
end
Or something like the above to catch this special case.
댓글 수: 2
Geoff Hayes
2018년 5월 18일
Chris - unfortunately, I don't have access to your list of files so can't reproduce what is happening? Have you tried stepping through the code with the debugger to see what is happening when this error occurs? Or else, copy and paste the full list of filenames to this question.
Image Analyst
2018년 5월 17일
The usual strategy in cases like this is to break it into bite-sized chunks (intermediate variables) and look at each part. You'd see that you'll get the error message you did if the filename did not have a dot in it and the result of strfind() was empty. Here is the fix:
filename = {'no_dot'; 'hasDot.png'; 'has.twoDots.png'}
dotIndexes = zeros(length(filename), 1);
for k = 1 : length(filename)
thisFile = filename{k}
dotLocations = strfind(thisFile, '.')
if ~isempty(dotLocations)
dotIndexes(k) = max(dotLocations)
end
end
% Code below will FAIL:
% for k = 1 : length(filename)
% idx(k) = max(strfind(char(filename{k}),'.'))
% end
댓글 수: 0
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!