How to solve: Limits must be a 2-element vector of increasing numeric values.

71 views (last 30 days)
How to solve that problem:
Error using ylim (line 31)
Limits must be a 2-element vector of increasing numeric values.
Error in DisplayWeigh33tsFinal (line 57)
ylim([0 MAX+0.2]);
This is the code:
function DisplayWeightsFinal(Weight_BWM_TOPSIS,...
Weight_GWO_TOPSIS,...
Weight_Combine_TOPSIS,FILENAME)
% {'Conversational','Background','Interactive','Streaming'}');
format long
global CostperByte;
global Security;
global DataRate;
global PacketDelay;
global PacketJitter;
global PacketLoss;
AHP= [0.101,0.195,0.297,0.092 ,0.119 ,0.192];
Weight_BWM_TOPSIS=[0.07,0.12,0.48,0.04,0.09,0.20];
Weight_GWO_TOPSIS=[0.10,0.10,0.4,0.1,0.1,0.2];
Weight_Combine_TOPSIS=[0.09,0.11,0.42,0.09,0.1,0.2];
CostperByteVector =[AHP(1,CostperByte) ,Weight_BWM_TOPSIS(1,CostperByte),Weight_GWO_TOPSIS(1,CostperByte),Weight_Combine_TOPSIS(1,CostperByte) ];
SecurityVector =[AHP(1,Security) ,Weight_BWM_TOPSIS(1,Security),Weight_GWO_TOPSIS(1,Security),Weight_Combine_TOPSIS(1,Security) ];
DataRateVector = [AHP(1,DataRate) ,Weight_BWM_TOPSIS(1,DataRate),Weight_GWO_TOPSIS(1,DataRate),Weight_Combine_TOPSIS(1,DataRate) ];
PacketDelayVector = [AHP(1,PacketDelay) ,Weight_BWM_TOPSIS(1,PacketDelay),Weight_GWO_TOPSIS(1,PacketDelay),Weight_Combine_TOPSIS(1,PacketDelay) ];
PacketJitterVector =[AHP(1,PacketJitter),Weight_BWM_TOPSIS(1,PacketJitter),Weight_GWO_TOPSIS(1,PacketJitter),Weight_Combine_TOPSIS(1,PacketJitter) ];
PacketLossVector =[AHP(1,PacketLoss) ,Weight_BWM_TOPSIS(1,PacketLoss),Weight_GWO_TOPSIS(1,PacketLoss),Weight_Combine_TOPSIS(1,PacketLoss) ];
MAX= max( [Weight_BWM_TOPSIS(1,CostperByte),Weight_GWO_TOPSIS(1,CostperByte),Weight_Combine_TOPSIS(1,CostperByte),...
Weight_BWM_TOPSIS(1,Security),Weight_GWO_TOPSIS(1,Security),Weight_Combine_TOPSIS(1,Security),...
Weight_BWM_TOPSIS(1,DataRate),Weight_GWO_TOPSIS(1,DataRate),Weight_Combine_TOPSIS(1,DataRate),...
Weight_BWM_TOPSIS(1,PacketDelay),Weight_GWO_TOPSIS(1,PacketDelay),Weight_Combine_TOPSIS(1,PacketDelay),...
Weight_BWM_TOPSIS(1,PacketJitter),Weight_GWO_TOPSIS(1,PacketJitter),Weight_Combine_TOPSIS(1,PacketJitter),...
Weight_BWM_TOPSIS(1,PacketLoss),Weight_GWO_TOPSIS(1,PacketLoss),Weight_Combine_TOPSIS(1,PacketLoss)]);
x = [1,2,3,4,5,6];
y = [CostperByteVector;SecurityVector;DataRateVector;PacketDelayVector;PacketJitterVector;PacketLossVector];
Bar = bar(x,y);
set(Bar, {'DisplayName'}, {'TOPSIS-AHP','TOPSIS-BWM','TOPSIS-GWO','TOPSIS-BWM-GWO'}');
% Legend will show names for each color
legend() ;
somenames={'C','S','DR','D','J','PLR'};
set(gca,'xticklabel',somenames);
set(gcf, 'Position', [100, 100, 990, 450])
ylabel('Weights')
xlabel('Decision Criteria')
ylim([0 MAX+0.2]);
opts = {'VerticalAlign','middle', 'HorizontalAlign','left', ...
'FontSize',8, 'Rotation',90};
for k1 = 1:4
ctr(k1,:) = bsxfun(@plus, Bar(k1).XData, Bar(k1).XOffset'); % Note: ‘XOffset’ Is An Undocumented Feature, This Selects The ‘bar’ Centres
ydt(k1,:) = Bar(k1).YData;
ydt(k1,:) % Individual Bar Heights
end
for k1 = 1:size(ctr,2)
text(ctr(:,k1), ydt(:,k1), sprintfc('%.2f',ydt(:,k1)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
end
grid on
%%
hold off
set(gcf,'Units','Inches');
pos = get(gcf,'Position');
set(gcf,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
print(gcf,"NEWPAPERTOPSIS",'-dpdf','-r0')
end

Answers (2)

Steven Lord
Steven Lord on 2 Feb 2023
Let's look at a couple sections of your code. I'm going to comment out the function declaration line so I can run this in Answers.
%{
function DisplayWeightsFinal(Weight_BWM_TOPSIS,...
Weight_GWO_TOPSIS,...
Weight_Combine_TOPSIS,FILENAME)
% {'Conversational','Background','Interactive','Streaming'}');
%}
format long
global CostperByte;
global Security;
global DataRate;
global PacketDelay;
global PacketJitter;
global PacketLoss;
AHP= [0.101,0.195,0.297,0.092 ,0.119 ,0.192];
Weight_BWM_TOPSIS=[0.07,0.12,0.48,0.04,0.09,0.20];
Weight_GWO_TOPSIS=[0.10,0.10,0.4,0.1,0.1,0.2];
Weight_Combine_TOPSIS=[0.09,0.11,0.42,0.09,0.1,0.2];
These three variables appear in the function declaration as inputs, but then you throw the values the user passed into the function away and use these hard-coded values. Did you intend the user to run this code (with their own values) before calling your function and pass these variables in? If so these lines shouldn't be in this function. If you intended to define these variables here, they shouldn't be specified as inputs to the function.
CostperByteVector =[AHP(1,CostperByte) ,Weight_BWM_TOPSIS(1,CostperByte),Weight_GWO_TOPSIS(1,CostperByte),Weight_Combine_TOPSIS(1,CostperByte) ];
SecurityVector =[AHP(1,Security) ,Weight_BWM_TOPSIS(1,Security),Weight_GWO_TOPSIS(1,Security),Weight_Combine_TOPSIS(1,Security) ];
DataRateVector = [AHP(1,DataRate) ,Weight_BWM_TOPSIS(1,DataRate),Weight_GWO_TOPSIS(1,DataRate),Weight_Combine_TOPSIS(1,DataRate) ];
PacketDelayVector = [AHP(1,PacketDelay) ,Weight_BWM_TOPSIS(1,PacketDelay),Weight_GWO_TOPSIS(1,PacketDelay),Weight_Combine_TOPSIS(1,PacketDelay) ];
PacketJitterVector =[AHP(1,PacketJitter),Weight_BWM_TOPSIS(1,PacketJitter),Weight_GWO_TOPSIS(1,PacketJitter),Weight_Combine_TOPSIS(1,PacketJitter) ];
PacketLossVector =[AHP(1,PacketLoss) ,Weight_BWM_TOPSIS(1,PacketLoss),Weight_GWO_TOPSIS(1,PacketLoss),Weight_Combine_TOPSIS(1,PacketLoss) ];
MAX= max( [Weight_BWM_TOPSIS(1,CostperByte),Weight_GWO_TOPSIS(1,CostperByte),Weight_Combine_TOPSIS(1,CostperByte),...
Weight_BWM_TOPSIS(1,Security),Weight_GWO_TOPSIS(1,Security),Weight_Combine_TOPSIS(1,Security),...
Weight_BWM_TOPSIS(1,DataRate),Weight_GWO_TOPSIS(1,DataRate),Weight_Combine_TOPSIS(1,DataRate),...
Weight_BWM_TOPSIS(1,PacketDelay),Weight_GWO_TOPSIS(1,PacketDelay),Weight_Combine_TOPSIS(1,PacketDelay),...
Weight_BWM_TOPSIS(1,PacketJitter),Weight_GWO_TOPSIS(1,PacketJitter),Weight_Combine_TOPSIS(1,PacketJitter),...
Weight_BWM_TOPSIS(1,PacketLoss),Weight_GWO_TOPSIS(1,PacketLoss),Weight_Combine_TOPSIS(1,PacketLoss)]);
Now let's look at what MAX is.
MAX
MAX = 1×0 empty double row vector
Why is this? global. From that documentation page: "If the global variable does not exist the first time you issue the global statement, it is initialized to an empty 0x0 matrix."
I strongly advise you not to use global variables. Consider parameterizing your function instead.
Why does MAX being 1x0 cause a problem?
inputToYlim = [0 MAX+0.2]
inputToYlim =
0
As the error message calls out, this is not a 2-element vector and so is not a valid input to ylim.
  2 Comments
Steven Lord
Steven Lord on 2 Feb 2023
It's possible. But there's no way to know based on the code that was posted. And that's the point. Anyone with access to the global namespace (i.e. any MATLAB code) can completely break this function due to its use of global variables. Trying to figure out who assigned to those global variables (or perhaps intended to assign to one of those variables but had a typo in the variable name, for example) and where could be difficult.

Sign in to comment.


Image Analyst
Image Analyst on 2 Feb 2023
It seems like MAX is a vector or matrix. Try this:
ylim([0, max(MAX, 'all') + 0.2]);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!