i have a code pasted below but each time i try to run it, i do get erro message in line 23 which i can not figure out.I always get this error message:Index exceeds matrix dimensions. Error in plotter_analysis (line 23) d_tm=diff(t_m); dtm=d_

1 次查看(过去 30 天)
[s]=size(r1);
% find(s==1)
if strcmp(sens_arg,'US')
US_analyzer
end
if strcmp(sens_arg,'nsensor')
tt_length=length(t_m);
nlength=length(n5_vec);
llength=length(lambda);
for icount=1:tt_length
imagesc(squeeze(r1(icount,:,:))');
getframe(gcf)
% pause(0.5)
end
end
%%Assume MIP sensor is being analyzes
if strcmp(sens_arg,'MIP')
if length(t_m)>0
d_tm=diff(t_m); dtm=d_tm(1);
else
dtm=1;
end
tt_length=length(t_t);
nlength=length(n5_vec);
llength=length(lambda);
for icount=1:tt_length
aaa=squeeze(r1(icount,:,:));
maxer(icount)=max(max(aaa));
max_sen(icount)=max(max(abs(diff(aaa,1,1))));
end
maxing=max(maxer);
maxings=max(max_sen);
for icount=1:tt_length
close all
if length(t_t)>1
figure(1)
subplot(2,1,1)
imagesc([t_m(1) t_m(length(t_m))],...
[lambda(1) lambda(length(lambda))],...
squeeze(r1(icount,:,:))./maxing);
title(['Dönüþtürücü çýktýsý t_t=', num2str(t_t(icount)),' nm'])
ylabel('\lambda [nm]');
xlabel('MDP kalýnlýðý [nm]')
caxis([0 1])
colorbar
subplot(2,1,2)
imagesc([t_m(1) t_m(length(t_m))],...
[lambda(1) lambda(length(lambda))],...
abs(diff(squeeze(r1(icount,:,:)),1,1))./dtm);
title(['Dönüþtürücü duyarlýlýðý t_t=', num2str(t_t(icount)),' nm'])
ylabel('\lambda [nm]');
xlabel('MDP kalýnlýðý [nm]')
caxis([0 maxings./dtm])
colorbar
else
figure(1)
imagesc([lambda(1) lambda(length(lambda))],...
[t_m(1) t_m(length(t_m))],...
squeeze(r1)./maxing);
title(['Farklý MDP kalýnlýklarýnda yansýma katsayýlarý'])
xlabel('\lambda [nm]');
ylabel('MDP kalýnlýðý [nm]')
caxis([0 1])
colormap('hot')
colorbar
figure(2)
imagesc([lambda(1) lambda(length(lambda))],...
[t_m(1) t_m(length(t_m))],...
abs(diff(squeeze(r1),1,1))./dtm);
title(['Dönüþtürücü duyarlýlýðý'])
xlabel('\lambda [nm]');
ylabel('MDP kalýnlýðý [nm]')
colormap('hot')
caxis([0 maxings])
colorbar
end
getframe(gcf)
pause(0.5)
end
end

采纳的回答

Stephen23
Stephen23 2016-2-17
编辑:Stephen23 2016-2-17
Solution
Change line 22:
length(t_m)>0
to this:
length(t_m)>1
I would also recommend that you use numel instead of length:
numel(t_m)>1
You should also put the following two allocations on two lines, because
  • they look ugly on one line.
  • having them on one line has made your debugging more complicated because the error message only gives the line and you have two allocations on one line: it is clearer with one allocation per line:
d_tm = diff(t_m);
dtm = d_tm(1);
Explanation
When t_m is scalar (i.e. has one element), then diff(t_m) is empty. Then you try to access the first element of an empty vector: d_tm(1) Bingo, error!
Note that you could have easily found this yourself by using MATLAB's debugging tools. They are really very useful because you can solve problems like this one very quickly.
  5 个评论
JOHN ADETIBA
JOHN ADETIBA 2016-2-17
this is the full error message
Improper assignment with rectangular empty matrix.
Error in plotter_analysis (line 34) max_sen(icount)=max(max(abs(diff(aaa,1,1))));
Stephen23
Stephen23 2016-2-17
编辑:Stephen23 2016-2-17
An internet search engine solved this in two seconds: you are trying to allocate an empty array to a scalar location. Basically you are doing this:
max_sen(icount) = []
which is always going to be an error (for a scalar icount). Why is the RHS empty? Lets go backwards:
  • The output of diff must have zero size along the requested dimension.
  • therefore the input argument on that dimension must have size one (e.g. could be scalar).
  • in other words aaa must have size 1x....
  • we confirm this because the preceding line does not cause an error: maxer(icount)=max(max(aaa));, and theiy differ in the size along that dimension.
It is clear that whoever wrote it:
  1. did not test their code as they wrote it,
  2. did not consider the sizes of matrices,
  3. did not consider edge cases.
Many beginners write large quantities of code without testing as they are writing, and then are clueless how to fix the multiple problems when they try to run it. Avoid doing this by always considering those three points above. Code that works is better than lots of code.
Oh, and learn to use the debugging tools, there are tutorials online.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by