Clear Filters
Clear Filters

인덱스가 배열 요소 개수를 초과합니다. 인덱스는 0을(를) 초과해서는 안 됩니다. 오류 해결 방법 있을까요 ?

92 views (last 30 days)
우근
우근 on 16 Apr 2024 at 15:44
Answered: Voss on 16 Apr 2024 at 17:45
%define surfaces to calculate distances
ab.BL = 0;
ab.SR = 0.4572;
ab.SL = -0.4572;
ab.TF = 2.2860;
ab.F = 2.0574;
bdStraight.AB = 0.24;
bdStraight.BL = -0.24;
bdStraight.BK = 0.32;
bdStraight.FR = -0.32;
bdTee.AB = 0.72;
bdTee.BL = -0.24;
bdTee.FL = -0.32;
bdTee.FU = -0.32;
bdElbow.BL = -0.24;
bdElbow.FR = -0.32;
bdElbow.BK = 0.32;
bdElbow.RT = 4.24;
plotShapes = {'-s','-o','-*','-^','-x','-p','-.'};
%[filename,path] = uigetfile('*.csv');
path = uigetdir;
cd(path);
files = dir(fullfile(path, '*_line.csv'));
for f=1:length(files)
figure('visible','off');
filename = files(f).name;
%use filename to choose geometry
config = regexp(filename,'c(.*)_', 'tokens');
if size(config)==0
config{1,1}{1,1}='';
end
geometry = eval(strcat(filename(1:2),config{1,1}{1,1}));
table = readtable(strcat(path,'/',filename));
data = table2array(table);
[rows, columns] = size(data);
%get list of DEVC locations
locations = fieldnames(geometry);
%iterate through locations
for i = 1:length(locations)
X=zeros(1,1);
Y=zeros(1,1);
%get a logical array of columns relevant to location
idx = strncmp(data(1,:),locations{i},length(locations{1}));
%ignore the columns with distances
idx(1:2:end)=0;
%get the indices of the logical array
indices = find(idx(1,:)==1);
%iterate through the planes of DEVCs at each distance and take max
%incident energy
for j = 2:rows
if not(strcmp(data{j,(indices(1)-1)},'') | strcmp(data{j,(indices(1)-1)},'NaN'))
X(j-1) = abs(cellfun(@str2num,data(j,(indices(1)-1)))-geometry.(locations{i}));
Y(j-1) = max(cellfun(@str2num,data(j,idx)))/1000;
end
end
plot(X,Y,plotShapes{i})
hold on
syms x;
curve = fit(X',Y','Exp2');
%plot(curve);
TScurve = @(x)30-curve(x);
TPcurve = @(x)15-curve(x);
tsZOI(i) = fzero(TScurve,0);
tpZOI(i) = fzero(TPcurve,0);
end
plotTitle = regexprep(filename,'_line\.csv','');
plotTitle = regexprep(plotTitle,'_',' ');
title(plotTitle)
xlabel('Distance from Surface (m)')
ylabel('Incident Energy (MJ/m^{2})')
plot(xlim, 15*[1 1])
plot(xlim, 30*[1 1])
legend(locations)
set(gca,'FontSize',15)
yl = ylim;
for i=1:length(locations)
text(1.5,(0.8*yl(2))-(0.08*yl(2)*i),strcat(locations{i},': TS: ',num2str(tsZOI(i)),'m',' TP: ',num2str(tpZOI(i)),'m'));
end
print(regexprep(filename,'.csv','.pdf'),'-dpdf')
end
  1 Comment
Voss
Voss on 16 Apr 2024 at 17:27
(I've removed uigetdir since it doesn't run in Answers, used the current directory '.' as the "path" variable, and renamed it to "my_path" since path is a built in MATLAB function.)
The problem is that data is a numeric matrix, but for some reason you are using it in strncmp:
idx = strncmp(data(1,:),locations{i},length(locations{1}));
I'm not sure what you expect the outcome of that to be, but idx is false, thus indices:
indices = find(idx(1,:)==1);
is empty, so the error is thrown when trying to access indices(1), which doesn't exist:
if not(strcmp(data{j,(indices(1)-1)},'') | strcmp(data{j,(indices(1)-1)},'NaN'))
In that line as well, you seem to expect data to be something other than numeric, specifically a cell array or table, based on the use of curly braces {} to index it.
%define surfaces to calculate distances
ab.BL = 0;
ab.SR = 0.4572;
ab.SL = -0.4572;
ab.TF = 2.2860;
ab.F = 2.0574;
bdStraight.AB = 0.24;
bdStraight.BL = -0.24;
bdStraight.BK = 0.32;
bdStraight.FR = -0.32;
bdTee.AB = 0.72;
bdTee.BL = -0.24;
bdTee.FL = -0.32;
bdTee.FU = -0.32;
bdElbow.BL = -0.24;
bdElbow.FR = -0.32;
bdElbow.BK = 0.32;
bdElbow.RT = 4.24;
plotShapes = {'-s','-o','-*','-^','-x','-p','-.'};
%[filename,path] = uigetfile('*.csv');
% path = uigetdir;
% cd(path);
my_path = '.';
files = dir(fullfile(my_path, '*_line.csv'));
for f=1:length(files)
figure('visible','off');
filename = files(f).name;
%use filename to choose geometry
config = regexp(filename,'c(.*)_', 'tokens');
if size(config)==0
config{1,1}{1,1}='';
end
geometry = eval(strcat(filename(1:2),config{1,1}{1,1}));
table = readtable(fullfile(my_path,filename));
data = table2array(table)
[rows, columns] = size(data);
%get list of DEVC locations
locations = fieldnames(geometry);
%iterate through locations
for i = 1:length(locations)
X=zeros(1,1);
Y=zeros(1,1);
%get a logical array of columns relevant to location
idx = strncmp(data(1,:),locations{i},length(locations{1}))
%ignore the columns with distances
idx(1:2:end)=0;
%get the indices of the logical array
indices = find(idx(1,:)==1)
%iterate through the planes of DEVCs at each distance and take max
%incident energy
for j = 2:rows
if not(strcmp(data{j,(indices(1)-1)},'') | strcmp(data{j,(indices(1)-1)},'NaN'))
X(j-1) = abs(cellfun(@str2num,data(j,(indices(1)-1)))-geometry.(locations{i}));
Y(j-1) = max(cellfun(@str2num,data(j,idx)))/1000;
end
end
plot(X,Y,plotShapes{i})
hold on
syms x;
curve = fit(X',Y','Exp2');
%plot(curve);
TScurve = @(x)30-curve(x);
TPcurve = @(x)15-curve(x);
tsZOI(i) = fzero(TScurve,0);
tpZOI(i) = fzero(TPcurve,0);
end
plotTitle = regexprep(filename,'_line\.csv','');
plotTitle = regexprep(plotTitle,'_',' ');
title(plotTitle)
xlabel('Distance from Surface (m)')
ylabel('Incident Energy (MJ/m^{2})')
plot(xlim, 15*[1 1])
plot(xlim, 30*[1 1])
legend(locations)
set(gca,'FontSize',15)
yl = ylim;
for i=1:length(locations)
text(1.5,(0.8*yl(2))-(0.08*yl(2)*i),strcat(locations{i},': TS: ',num2str(tsZOI(i)),'m',' TP: ',num2str(tpZOI(i)),'m'));
end
print(regexprep(filename,'.csv','.pdf'),'-dpdf')
end
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
data = 32x608
1.0e+04 * -0.0000 0.0717 -0.0000 0.2841 -0.0000 0.2965 -0.0000 0.2613 -0.0000 0.0181 -0.0000 0.0186 -0.0000 0.0752 -0.0000 0.2976 -0.0000 0.3102 -0.0000 0.2727 -0.0000 0.0194 -0.0000 0.0201 -0.0000 0.0767 -0.0000 0.3031 -0.0000 0.3155 -0.0000 0.1070 -0.0000 0.2522 -0.0000 0.2523 -0.0000 0.2054 -0.0000 0.0179 -0.0000 0.0166 -0.0000 0.1121 -0.0000 0.2639 -0.0000 0.2638 -0.0000 0.2144 -0.0000 0.0192 -0.0000 0.0179 -0.0000 0.1142 -0.0000 0.2687 -0.0000 0.2684 -0.0000 0.1237 -0.0000 0.2225 -0.0000 0.2137 -0.0000 0.1635 -0.0000 0.0170 -0.0000 0.0147 -0.0000 0.1294 -0.0000 0.2327 -0.0000 0.2234 -0.0000 0.1706 -0.0000 0.0182 -0.0000 0.0159 -0.0000 0.1318 -0.0000 0.2370 -0.0000 0.2273 -0.0000 0.1289 -0.0000 0.1954 -0.0000 0.1808 -0.0000 0.1316 -0.0000 0.0158 -0.0000 0.0131 -0.0000 0.1348 -0.0000 0.2042 -0.0000 0.1889 -0.0000 0.1374 -0.0000 0.0168 -0.0000 0.0141 -0.0000 0.1374 -0.0000 0.2080 -0.0000 0.1923 -0.0000 0.1273 -0.0000 0.1710 -0.0000 0.1532 -0.0000 0.1073 -0.0000 0.0145 -0.0000 0.0118 -0.0000 0.1330 -0.0000 0.1787 -0.0000 0.1600 -0.0000 0.1119 -0.0000 0.0154 -0.0000 0.0126 -0.0000 0.1356 -0.0000 0.1821 -0.0000 0.1630 -0.0000 0.1216 -0.0000 0.1494 -0.0000 0.1302 -0.0000 0.0883 -0.0000 0.0133 -0.0000 0.0106 -0.0000 0.1270 -0.0000 0.1560 -0.0000 0.1359 -0.0000 0.0921 -0.0000 0.0141 -0.0000 0.0113 -0.0000 0.1295 -0.0000 0.1591 -0.0000 0.1385 -0.0001 0.1138 -0.0001 0.1304 -0.0001 0.1110 -0.0001 0.0734 -0.0001 0.0122 -0.0001 0.0097 -0.0001 0.1188 -0.0001 0.1361 -0.0001 0.1158 -0.0001 0.0765 -0.0001 0.0128 -0.0001 0.0102 -0.0001 0.1213 -0.0001 0.1389 -0.0001 0.1181 -0.0001 0.1049 -0.0001 0.1138 -0.0001 0.0950 -0.0001 0.0616 -0.0001 0.0112 -0.0001 0.0088 -0.0001 0.1096 -0.0001 0.1188 -0.0001 0.0991 -0.0001 0.0642 -0.0001 0.0117 -0.0001 0.0093 -0.0001 0.1119 -0.0001 0.1212 -0.0001 0.1011 -0.0001 0.0959 -0.0001 0.0994 -0.0001 0.0817 -0.0001 0.0521 -0.0001 0.0103 -0.0001 0.0081 -0.0001 0.1001 -0.0001 0.1037 -0.0001 0.0852 -0.0001 0.0542 -0.0001 0.0107 -0.0001 0.0085 -0.0001 0.1023 -0.0001 0.1060 -0.0001 0.0870 -0.0001 0.0870 -0.0001 0.0869 -0.0001 0.0705 -0.0001 0.0444 -0.0001 0.0094 -0.0001 0.0074 -0.0001 0.0908 -0.0001 0.0907 -0.0001 0.0735 -0.0001 0.0462 -0.0001 0.0098 -0.0001 0.0078 -0.0001 0.0929 -0.0001 0.0928 -0.0001 0.0752
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
idx = logical
0
indices = []
Index exceeds the number of array elements. Index must not exceed 0.

Sign in to comment.

Answers (1)

Voss
Voss on 16 Apr 2024 at 17:45
Assuming data is meant to be a cell array (and using readcell to make it such), adjusting the row indexing to account for the first two rows of data being "header" rows, and adjusting the syntax to account for the fact that data after the first two rows is numeric instead of chars - which involved removing str2num and cellfun - then I get something that runs.
Maybe this is what's intended, maybe not; please verify. (The figure was made visible to show the result.)
%define surfaces to calculate distances
ab.BL = 0;
ab.SR = 0.4572;
ab.SL = -0.4572;
ab.TF = 2.2860;
ab.F = 2.0574;
bdStraight.AB = 0.24;
bdStraight.BL = -0.24;
bdStraight.BK = 0.32;
bdStraight.FR = -0.32;
bdTee.AB = 0.72;
bdTee.BL = -0.24;
bdTee.FL = -0.32;
bdTee.FU = -0.32;
bdElbow.BL = -0.24;
bdElbow.FR = -0.32;
bdElbow.BK = 0.32;
bdElbow.RT = 4.24;
plotShapes = {'-s','-o','-*','-^','-x','-p','-.'};
%[filename,path] = uigetfile('*.csv');
% path = uigetdir;
% cd(path);
my_path = '.';
files = dir(fullfile(my_path, '*_line.csv'));
for f=1:length(files)
figure('visible','on');
filename = files(f).name;
%use filename to choose geometry
config = regexp(filename,'c(.*)_', 'tokens');
if size(config)==0
config{1,1}{1,1}='';
end
geometry = eval(strcat(filename(1:2),config{1,1}{1,1}));
data = readcell(fullfile(my_path,filename));
[rows, columns] = size(data);
%get list of DEVC locations
locations = fieldnames(geometry);
%iterate through locations
for i = 1:length(locations)
X=zeros(1,1);
Y=zeros(1,1);
%get a logical array of columns relevant to location
idx = strncmp(data(2,:),locations{i},length(locations{i}));
%ignore the columns with distances
idx(1:2:end)=0;
%get the indices of the logical array
indices = find(idx);
%iterate through the planes of DEVCs at each distance and take max
%incident energy
for j = 3:rows
if ~isnan(data{j,indices(1)-1})
X(j-2) = abs(data{j,indices(1)-1}-geometry.(locations{i}));
Y(j-2) = max([data{j,idx}])/1000;
end
end
plot(X,Y,plotShapes{i})
hold on
syms x;
curve = fit(X',Y','Exp2');
%plot(curve);
TScurve = @(x)30-curve(x);
TPcurve = @(x)15-curve(x);
tsZOI(i) = fzero(TScurve,0);
tpZOI(i) = fzero(TPcurve,0);
end
plotTitle = regexprep(filename,'_line\.csv','');
plotTitle = regexprep(plotTitle,'_',' ');
title(plotTitle)
xlabel('Distance from Surface (m)')
ylabel('Incident Energy (MJ/m^{2})')
plot(xlim, 15*[1 1])
plot(xlim, 30*[1 1])
legend(locations)
set(gca,'FontSize',15)
yl = ylim;
for i=1:length(locations)
text(1.5,(0.8*yl(2))-(0.08*yl(2)*i),strcat(locations{i},': TS: ',num2str(tsZOI(i)),'m',' TP: ',num2str(tpZOI(i)),'m'));
end
print(regexprep(filename,'.csv','.pdf'),'-dpdf')
end

Community Treasure Hunt

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

Start Hunting!