Subscript indices must either be real positive integers or logicals.

1 view (last 30 days)
x = linspace(0,10)
y1 = exp(x)
y2 = sin(x)
a = 5
b = 2
c = 4
y3 = a*x.^2 + b*x + c
y4 = sqrt(x)
plot (x,y1,'r--.',x,y2,'b--o',x,y3,'k--*' ,x,y4,'m--v')
xlabel
ylabel
i received an error showing 'Subscript indices must either be real positive integers or logicals.' what should i do?

Answers (2)

Image Analyst
Image Analyst on 14 Feb 2016
Anyway, try this (just copy and paste into a script):
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
x = linspace(0,10, 40) ;
y1 = exp(x) ;
y2 = sin(x)
a = 5;
b = 2;
c = 4;
y3 = a*x.^2 + b*x + c
y4 = sqrt(x)
plot(x,y1, 'r*-', 'LineWidth', 2)
hold on;
plot(x,y2, 'bo-', 'LineWidth', 2)
plot(x,y3, 'g^-', 'LineWidth', 2)
plot(x,y4, 'md-', 'LineWidth', 2)
xlabel('X', 'FontSize', fontSize)
ylabel('Y', 'FontSize', fontSize)
title('My Plot', 'FontSize', fontSize)
grid on;
legend('y1', 'y2', 'y3', 'y4')'
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
  3 Comments
Image Analyst
Image Analyst on 14 Feb 2016
No arguments provided to xlabel and ylabel. Other than that there are no errors.
Guillaume
Guillaume on 15 Feb 2016
"but what is wrong with mine?"
As pointed by IA, there is nothing wrong with your code other than the last two lines. If you get the error "Subscript indices must either be real positive integers or logicals" that can only be because you've got a variable called exp, or sin or sqrt or linspace| in your workspace.
See my answer for more details.

Sign in to comment.


Guillaume
Guillaume on 14 Feb 2016
Edited: Guillaume on 14 Feb 2016
In the code you've posted, the only parts that matlab could interpret as subscripting a variable are the function calls to linspace, exp, sin or sqrt. Most likely, you have a variable with one of these names. A variable with the same name as a function shadows that function which can no longer be called.
clear the offending variable and use a different name for it.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!