Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
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 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Geoff Hayes
le 17 Mai 2018
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 commentaires
Geoff Hayes
le 18 Mai 2018
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
le 17 Mai 2018
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 commentaires
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!