Error ??? Subscript indices must either be real positive integers or logicals.

2 Ansichten (letzte 30 Tage)
Xiaoyun Li
Xiaoyun Li am 19 Sep. 2012
Beantwortet: vanaja m am 10 Mär. 2014
I created a script to read a vector from the Excel file.
Script:
V = xlsread('C:\ad\fMRI\vbm_cat\subjectsdata\vbmcatsub.xls');
variables = ('Age FPQ CPS RUM MAG HEL STAI_A STAI_B SEX')
========================================
Then I get a vector V with the size of 36*9,
I need to get the values of each variable. so I type the commands then I get the following error message
>> V(i,1) ??? Subscript indices must either be real positive integers or logicals.
Why is this? How to fix it?
Thanks.

Antworten (5)

Shane
Shane am 19 Sep. 2012
First off, depending upon what kind of data you have in your excel file, you might need to use two ouputs in the xlsread function as shown: [numericValues textValues]=xlsread(‘FILE’) See help xlsread for why this is.
Secondly, the error message you have shown is because you have not defined the variable “i” to be an integer. In order to determine the first variable in the matrix V you would have to type V(1,1). If you simply want to display all of the variables you could use a for loop:
For i = 1:36
For j = 1:9
V(i,j)
End
End
Long story short, you just need to define your variables before trying to use them as a callout.
I hope this helped :)
  2 Kommentare
Xiaoyun Li
Xiaoyun Li am 19 Sep. 2012
The data of Excel file is numerical data, so the matrix V is as follows >> V
V =
26 63 7 5 2 0 29 45 2
24 67 16 6 4 6 28 40 1
28 58 3 2 0 1 36 36 1
30 52 3 2 0 1 22 31 2
29 66 21 10 3 8 31 34 2
30 94 10 6 0 4 42 49 1
27 85 8 3 3 2 27 36 2
32 85 4 1 2 1 30 32 2
25 66 7 4 1 2 22 28 1
22 90 16 7 5 4 36 40 1
32 123 21 7 3 11 29 44 2
21 60 30 13 7 10 25 33 1
19 72 18 9 4 5 47 51 1
31 75 25 10 6 9 24 24 1
19 88 18 6 11 1 36 39 1
32 94 4 1 2 1 34 43 2
20 69 34 13 9 12 26 30 1
40 76 12 4 1 7 32 41 2
23 100 23 8 8 7 46 47 1
21 55 18 8 2 8 23 27 2
20 79 15 7 3 5 43 37 1
32 77 4 1 0 3 39 45 1
41 78 9 4 1 4 26 34 1
51 77 5 4 1 0 20 24 1
47 95 5 0 0 5 32 38 1
47 42 2 1 1 0 23 25 1
37 51 0 0 0 0 20 38 1
38 85 11 6 1 4 31 31 1
38 70 0 0 0 0 50 45 1
49 82 1 1 0 0 23 43 1
41 80 16 4 1 11 26 40 1
22 60 1 1 0 0 28 36 1
27 67 4 2 1 1 37 36 1
48 89 4 1 1 2 27 34 1
31 75 19 8 3 8 25 43 1
45 94 0 0 0 0 21 28 1
Each column stands for one variable and each row stands for one participant, so here has 9 variables, and 36 pariticpants.
If I need to get the data only for one variable, what shold I do? Thnaks.
Shane
Shane am 19 Sep. 2012
If you want a single variable for ALL participants, use
desiredVariable = V(:,columnNumber)
If you want a single variable for participants 10 through 15 for instance, use
desiredVariable = V(10:15,columnNumber)

Melden Sie sich an, um zu kommentieren.


Andrei Bobrov
Andrei Bobrov am 19 Sep. 2012
Bearbeitet: Andrei Bobrov am 19 Sep. 2012
use struct - array:
out = cell2struct(num2cell(V),regexp(variables,'\w*','match'),2);
or
out = cell2struct(num2cell(V,1),regexp(variables,'\w*','match'),2);
or
data = [regexp(variables,'\w*','match');num2cell(V,1)];
out = struct(data{:});
or
data = [regexp(variables,'\w*','match');num2cell(num2cell(V),1)];
out = struct(data{:});

Matt Tearle
Matt Tearle am 19 Sep. 2012
Bearbeitet: Matt Tearle am 19 Sep. 2012
To get the fourth variable:
RUM = V(:,4);
The : as the first index represents all rows.
Given your description of the data, you might also want to consider using a dataset array (requires Statistics Toolbox).
V = dataset('XLSFile','filename.xls');
Then you can refer to variables by name: V.RUM (assuming there is a header row in the Excel file that can be used to define the variable names).
  1 Kommentar
Shane
Shane am 19 Sep. 2012
The statistics toolbox is a powerful tool but if you are just going to be doing basic data analyses I would suggest sticking with the standard annotation as shown by
RUM = V(:,4);

Melden Sie sich an, um zu kommentieren.


Xiaoyun Li
Xiaoyun Li am 19 Sep. 2012
Thanks for all.
Actually, I need to use these variables as covariates in the regression of SPM. I try the way you mentioned before V(:,columnNumber). I can get the whole column of the variable which I want. However, when I transfer these values into the SPM8 and run the analysis, the statiscial analysis showed me that Beta not uniquely specified. That is why I concerned if the commands for getting single variable is wrong, because I need these values as covariate values in the vector. However, it seems the proble is still there.

vanaja m
vanaja m am 10 Mär. 2014
i too got the same problem while performing speaker recognition using vq method.......here is the code and the error is ??? Subscript indices must either be real positive integers or logicals.
Error in ==> vqlbg at 30 c(:, i) = mean(d(:, (find(ind == j))), 2); code: clc clear all; close all; code=train('C:\Users\welcome\Desktop\project\matlab\train\',8); test('C:\Users\welcome\Desktop\project\matlab\test\',8,code); train.m: function code = train(traindir, n)
k = 16;
for i = 1:n
file = sprintf('%ss%d.wav', traindir, i);
disp(file);
[s, fs] = wavread(file);
size(s)
v = melfcc(s, fs); % Compute MFCC's
code{i} = vqlbg(v, k); % Train VQ codebook
end
test.m
function test(testdir, n, code)
for l = 1:length(code) % each trained codebook, compute distortion
% code{l}(end:numel(v))=0;
d = disteu(v, code{l});
dist = sum(min(d,[],2))/size(d,l);
if dist1< distmin
distmin = dist;
k1 = l;
end
end
msg = sprintf('Speaker %d matches with speaker %d', k, k1);
disp(msg);
melfcc.m:
function r = melfcc(s, fs1)
m=160;
n=256;
frames=blockFrames(s,fs1,m,n);
t=n/2;
tmax=length(s)/fs1;
m=melfb(n,16,fs1)
n2=1+floor(n/2)
length(m);
length(frames)
disp(frames)
% z=m*abs(frames(1:n2,:)).^2;
r=dct(log(abs(frames)));
end
blockFrames.m:
function M3 = blockFrames(s, fs, m, n)
l = length(s);
nbFrame = floor((l - n)/m) + 1;
for i = 1:n
for j = 1:nbFrame
M(i, j) = s(((j - 1) * m) + i);
end
end
h = hamming(n);
M2 = diag(h) * M;
for i = 1:nbFrame
M3(:, i) = fft(M2(:, i));
end
end
size(code{1})
melfb.m:
function m = melfb(p, n, fs)
f0 = 700 / fs;
fn2 = floor(n/2);
lr = log(1 + 0.5/f0) / (p+1);
bl = n * (f0 * (exp([0 1 p p+1] * lr) - 1));
b1 = floor(bl(1)) + 1;
b2 = ceil(bl(2));
b3 = floor(bl(3));
b4 = min(fn2, ceil(bl(4))) - 1;
pf = log(1 + (b1:b4)/n/f0) / lr;
fp = floor(pf);
pm = pf - fp;
r = [fp(b2:b4) 1+fp(1:b3)];
c = [b2:b4 1:b3] + 1;
v = 2 * [1-pm(b2:b4) pm(1:b3)];
m = sparse(r, c, v, p, 1+fn2);
end
disteu.m:
function d = disteu(x, y)
[M, N] = size(x);
[M2, P] = size(y);
if (M ~= M2) error('Matrix dimensions do not match.') end
d = zeros(N, P);
if (N < P) copies = zeros(1,P); for n = 1:N d(n,:) = sum((x(:, n+copies) - y) .^2, 1); end else copies = zeros(1,N); for p = 1:P d(:,p) = sum((x - y(:, p+copies)) .^2, 1)'; end end
d = d.^0.5; end vqlbg.m: function c =vqlbg(d, k) % VQLBG Vector quantization using the Linde-Buzo-Gray algorithm % % Inputs: % d contains training data vectors (one per column) % k is number of centroids required % % Outputs: % c contains the result VQ codebook (k columns, one for each % centroids)
%Hints
%Cluster Vectors (Nearest-Neighbor Search): c=mean(d,2);
%The nearest-neighbor search step is: given a current codebook c, assign each training vector in d with a closest codeword. To do that, one needs to compute the pair-wise distances between each vectors in d to each vectors (codeword) in c. This can be done with the supplied function disteu: z = disteu(d, c);
%Now z(i, j) would be the distance between the training vector d(:, i) and the codeword c(:, j). Next step, for each training vector, find the closest codeword. For this, use the Matlab function min:
[m, ind] = min(z, [], 2);
%The result index vector ind contains the associated cluster number for each training vector. So to access to all the training vectors that belong to the cluster number j (those vectors that are closest to the codeword c(:, j)), one can use:
d(:, find(ind == j));
%Find Centroids
%The centroid of all vectors in a particular cluster is found by the Matlab function mean. For example, after the Nearest-Neighbor Search step above, the new centroids of the clusters number j can be updated as follows: c(:, i) = mean(d(:, (find(ind == j))), 2); % c(:, j) = mean(d(:, b, 2)); end plssssssssssssshelp urgent

Kategorien

Mehr zu Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by