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!