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.
2 次查看(过去 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!