How to fix the Dot indexing is not supported for variables of this type error?
1 次查看(过去 30 天)
显示 更早的评论
clc;
clear all;
close all;
format long g;
load('dtm.mat')
dist_hwZ= repmat([0:100:(size(Z,1)-1)*100]',1,size(Z,2));
dist_rwZ = (repmat([0:100:(size(Z,2)-1)*100]',1,size(Z,1),1))';
thinned_dist_hwZ = dist_hwZ(1:5:end,1:5:end);
thinned_dist_rwZ = dist_rwZ(1:5:end,1:5:end);
% thinned_dist_rwZ = (thinned_dist_rwZ)';
thinned_Z = Z(1:5:end,1:5:end);
current_dist = NaN(size(thinned_dist_hwZ));
assigned_dist = current_dist; %line23
for i = 1:length(thinned_dist_hwZ,1)
for j=1:length(thinned_dist_hwZ,2)
current_dist = sqrt(((thinned_dist_hwZ-thinned_dist_hwZ(i,j)).^2 + (thinned_dist_rwZ-thinned_dist_rwZ(i,j)).^2)');
%compute distance not height % sqrt delx^2 dely^2 approx 70 km distance of hypotenous
if i==1 && j==1
max_dist=max(max(current_dist));
end
for k =1:round(max_dist/1e4)
if k==1
assigned_dist(current_dist<=k*1e4)=k;
else
assigned_dist(current_dist<=k*1e4 & current_dist>(k-1)*1e4)=k;
end
prod_Z = Z(assigned_dist==k).*Z(i,j);
eval(['allClasses.i' num2str(i) ['.j' num2str(j) ['.k' num2str(k) '=prod_Z' ]]]);
if k==1
eval(['K1:prod_Z_' [num2str(i) '_' num2str(j)] '=prod_Z']);
elseif k==2
eval(['K2:prod_Z_' [num2str(i) '_' num2str(j)] '=prod_Z']);
elseif k==3
eval(['K3:prod_Z_' [num2str(i) '_' num2str(j)] '=prod_Z']);
elseif k==4
eval(['K4:prod_Z_' [num2str(i) '_' num2str(j)] '=prod_Z']);
elseif k==5
eval(['K5:prod_Z_' [num2str(i) '_' num2str(j)] '=prod_Z']);
elseif k==6
eval(['K6:prod_Z_' [num2str(i) '_' num2str(j)] '=prod_Z']);
else
eval(['K7:prod_Z_' [num2str(i) '_' num2str(j)] '=prod_Z']);
end
end
imagesc(current_dist)
end
end
fns = fieldnames(k);
1 个评论
Stephen23
2021-6-11
编辑:Stephen23
2021-6-11
"How to fix the Dot indexing is not supported for variables of this type error?"
Better code. Your design forces you into writing slow, inefficient, complex code which is liable to bugs and difficult to debug. This is exactly what you are facing now. You can avoid this by designing your data better and using more reliable and efficient code paradigms:
For example, this line:
eval(['allClasses.i' num2str(i) ['.j' num2str(j) ['.k' num2str(k) '=prod_Z' ]]]);
what is the point in having these multiple nested horizontal concatenations? Why do you need superfluous EVAL when fieldnames can be specified directly?:
Even better would be to avoid forcing meta-data into fieldnames (slow) and use arrays instead (fast, could be numeric or container class, e.g. structure). Bad code also makes it easy to hide bugs:
K7:prod_Z_1_1 = prod_Z
^ What do you expect this syntax to achieve?
Better data design allows for simpler, more efficient code, with fewer bugs.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!