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

回答(2 个)

Geoff Hayes
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 个评论
Chris Park
Chris Park 2018-5-17
Hi Geoff, Thank you for your help and time. I tried what you suggested but was unable to find a solution. I included the code up to the point where I was having the error if you wouldn't mind taking a look at it. Chris
%clc
%clear all
%close all
%Open List Files
[filename1,pathname1] = uigetfile({'*.xlsx'},'Select Schedule');
if filename1 == 0
return
else
filename{1} = filename1;
pathname{1} = pathname1;
end
%Import data from an excel file.
fullpathname{1} = strcat (pathname{1}, filename{1});
dataDyno = xlsread(fullpathname{1},1);
total_test_time(1) = dataDyno(length(dataDyno(:,1)),1);
time_hours = round(total_test_time(1)/(3600)*100)/100;
[filenameCAE, pathnameCAE] = uigetfile({'*.xlsx'},'CAE Data File Selector');
fullpathname{2} = strcat (pathnameCAE, filenameCAE);
caeData = xlsread(fullpathname{2},1);
[filenameSN, pathnameSN] = uigetfile({'*.xlsx'},'Material Data File Selector');
fullpathname{3} = strcat (pathnameSN, filenameSN);
materialData = xlsread(fullpathname{3},1);
[filename4,pathname4] = uigetfile({'*.xlsx'},'10x10 File Selector');
if filename4 ~= 0
filename{4} = filename4;
pathname{4} = pathname4;
end
fullpathname{4} = strcat (pathname{4}, filename{4});
dataVeh_10 = flipud(xlsread(fullpathname{4},1));
time_10_x_10 = CEM/veh_speed;
total_test_time_10(4) = time_10_x_10*3600;
time_hours_veh = round(time_10_x_10);
dataveh_time_frac = dataVeh_10;
%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
%%Data Storing/Sorting
%try
%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
Geoff Hayes
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
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

此问题已关闭。

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by