Subscript indices must either be real positive integers or logicals.

Hi,
I see I am really bad at indexing so if someone could tell me some general rules (but less general than in online tutorials) I would be grateful.
*Subscript indices must either be real positive integers or logicals.
Error in PL2ex1 (line 4) meandata = mean(nonoise.data_out);*
File to read was uploaded.
%PL2ex1
nonoise = load('verg1');
meandata = mean(nonoise.data_out);
trials = repmat(meandata, 1000, 1);
%add noise with normal distribution (randn) and repeat
sigma = randn(1);
for i = 1:1000
trials(i) = trials(i) + randn(1)*randn(size(trials(i)));
end
%compare plots and mean of trials
x = linspace(0, 10); % define axis x
figure
subplot(3,3,1)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,2)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,3)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,4)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,5)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,6)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,7)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
subplot(3,3,8)
n =(randi(1000));
plot(x, trials(n))
title(sprintf('Trial %d', n))
%mean
subplot(3,3,9)
means = zeros(1,400);
for i = 1:400
means(1,i) = mean(trials(:,i));
end
plot(x, means)
title('Means')

 Accepted Answer

In the Command Window, type:
whos mean
The output should be nothing (just the >> cursor).
If you get something like this:
Name Size Bytes Class Attributes
mean 1x1 8 double
that means you have assigned a variable to ‘mean’, ‘overshadowing’ the mean function.
Solution: Rename the variable.

11 Comments

I had problem like this before posting a question (I came up with such idea on my own), I edited and saved file, clear all but it seems MATLAB needed something more because after typing whos mean (with no result) and trying to run again there was no more error. I am sorry for bothering you, I thought it was something more serious than 'quit and run again' because I tried many times before.
It works but not well, so if I can ask, why my plots (excluding mean) are blank? They are blank whatever I set as x or when x is removed from plot, axes are adjusted but I see no points.
Please attach verg1.mat so we can try your code, both to fix the plotting problem and to see if there is a mean inside it. Generally it's safer to assign load() a structure than to just let it "poof" variables into existence.
s = load('verg1.mat'); % Read variables into fields of a structure.
myMean = s.mean;
Of course there should not even be a variable called mean in your mat file, but unfortunately it seems that is the most likely cause of the error.
What size is ‘trials’ when you actually go to plot it?
Note that:
x = linspace(0, 10); % define axis x
produces a 100-element vector from 0 to |10}, but in every plot:
n =(randi(1000));
is a single random integer between 1 and 1000.
Image Analyst: It is attached from the beginning. I predicted such need :)
Star Strider: What I wanted to do was:
trials = repmat(meandata, 1000, 1);
- 1000 copies of meandata with noise added later
n =(randi(1000));
plot(x, trials(n))
- plot random trial
Where is my mistake?
Put simply:
n =(randi(1000));
is a single random integer between 1 and 1000. It is not a vector.
I wanted to display one of copies (copy number random n) from array 'trials', why it doesn't work?
Since ‘trials’ is a (1000x400) matrix, you have to specify that you want a row and not a single element.
You will also need to define ‘x’ as a 400-element vector.
See if this does what you want:
x = linspace(0, 10, 400); % define axis x
then:
plot(x, trials(n,:))
Now I have:
Index exceeds matrix dimensions.
Error in PL2ex1 (line 20) title(sprintf('Trial %d', n))
I don't understand why, I didn't change anything concerning n :(
I don’t know either. I assume ‘n’ is an integer between 1 and 1000. You might see if changing ‘%d’ to ‘%.0f’:
title(sprintf('Trial %.0f', n))
changes anything.
I can’t see how that line would throw that error.
No errors now, but the same graph in every subplot -.- It is so confusing.
See if this slight modification of your code — that does all your first 8 plots in the for loop — improves it:
nonoise = load('verg1');
meandata = mean(nonoise.data_out);
trials = repmat(meandata, 1000, 1);
x = linspace(0, 10, 400); % define axis x
trials = trials + randn(size(trials))*0.5;
for k1 = 1:8
subplot(3,3,k1)
n = randi(1000);
plot(x, trials(n,:))
title(sprintf('Trial %d', n))
end
%mean
subplot(3,3,9)
means = mean(trials);
plot(x, means)
title('Means')
Experiment with different constants (actually, the standard deviation, here 0.5) to get the randomness you want. you will see that the plots are now all different. I believe the noise you were adding was too small to be visible on your plots.

Sign in to comment.

More Answers (1)

I would guess that somewhere in your system you have defined a variable named 'mean', in which case matlab misinterprets the calls to the function 'mean' as referring to that variable. That is the only way I can account for the error message. You should never use the names of matlab functions as variables for just this reason.

Community Treasure Hunt

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

Start Hunting!