MATLAB Answers

Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

12 views (last 30 days)
Dear community, help with this.
Voc=Clean(:,7);
a=Voc(1:3);
j=1;k=9;
t=length(Voc);
for i=1:t;
v= Voc(j:k,1);
a=cellfun(@mean,v);
Voc_p(i,1)=a;
j=j+9;
k=k+9;
end
A datatable was convert into a array previously and appears this error:
Unable to perform assignment because the indices on the left side are not compatible with the size of the
right side.
Error in Prueba_1 (line 29)
Voc_p(i,1)=a;
How can I fix this?
Thank u

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jun 2019
v= Voc(j:k,1);
That is creating a non-scalar cell array
a=cellfun(@mean,v);
That is going through each cell member and taking the mean of each. The fact that you did not get an error on this line tells us that each cell member was a scalar or a vector, not empty and not 2 D or more. The end result will be a vector of length k-j+1 = 9.
Voc_p(i,1)=a;
You are trying to store that vector of length 9 into a single location, Voc_p(i,1)

  13 Comments

Jonathan Bijman
Jonathan Bijman on 11 Jun 2019
clc
clear all
datadir = 'C:\Users\Jonat\Desktop\Clean';
dinfo = dir( fullfile(datadir, '*.xlsx') );
filenames = fullfile( datadir, {dinfo.name} );
numfiles = length(filenames);
datatable = [];
for K = 1 : numfiles
thisfile = filenames{K};
thistable = readtable(thisfile);
if K == 1
datatable = thistable;
else
datatable = [datatable; thistable];
end
end
Clean=table2cell(datatable);
Voc=Clean(:,7);
a=Voc(1:3);
nrow = height(datatable );
lastidx = floor((nrow/9) * 9);
Voc_p = mean( reshape(datatable{1:lastidx,7}, 9, []) );
that's the whole script I'm using
Jonathan Bijman
Jonathan Bijman on 11 Jun 2019
It works!
Thank U so much Walter for all the troubles I caused u.
I hope can I count for your help in the future, for this amateur like me.
Thank u so much =)

Sign in to comment.

More Answers (0)