Subscript indices must either be real positive integers or logicals.
11 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
回答 (2 件)
Image Analyst
2016 年 2 月 14 日
The FAQ explains it pretty well: http://matlab.wikia.com/wiki/FAQ#How_do_I_fix_the_error_.22Subscript_indices_must_either_be_real_positive_integers_or_logicals..22.3F
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 件のコメント
Image Analyst
2016 年 2 月 14 日
No arguments provided to xlabel and ylabel. Other than that there are no errors.
Guillaume
2016 年 2 月 15 日
"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.
Guillaume
2016 年 2 月 14 日
編集済み: Guillaume
2016 年 2 月 14 日
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!