Dimensions of arrays being concatenated are not consistent.

6 views (last 30 days)
%% Connection
a = arduino('COM4', 'uno');
%%Parameters
T = 100; % number of samples to view on plot
nCount = 1;
voltageS1 = zeros(T,1);
voltageS2 = zeros(T,1);
avrgVolt = zeros(T,1);
avrgVolt1 = zeros(T,1);
input=0;
while 1
for i= 1:1:100
if nCount == T
voltageS1 = voltageS1(2:end, :);
voltageS2 = voltageS2(2:end, :);
avrgVolt = avrgVolt(2:end, :);
avrgVolt1=avrgVolt1(2:end, :);
else
nCount = nCount + 1;
end
voltageS1(nCount,:) = readVoltage(a, 'A0'); % Sensor 1 connected to input A0 .
voltageS2(nCount,:) = readVoltage(a, 'A1'); % Sensor 2 connected to input A1 .
avrgVolt1(nCount,:) = ((voltageS1(nCount,:)/5)-(voltageS2(nCount,:)/5));
avrgVolt(nCount,:) = ((voltageS1(nCount,:))-(voltageS2(nCount,:)));
input=[input ,avrgVolt ]
dataPlot = [voltageS1, voltageS2, avrgVolt];
plot(1:T,dataPlot)
title(sprintf('A0 = %fV A1 = %fV Avg = %f avrgVolt1= %f', (voltageS1(nCount)), (voltageS2(nCount)) , avrgVolt(nCount,:), avrgVolt1(nCount,:)))
grid on;
axis([0 T -5 5])
xlabel('Samples') % x-axis label
ylabel('Voltage') % y-axis label
drawnow
end
  1 Comment
Jan
Jan on 3 Jul 2022
Please use the tools for code formatting and post a copy of the complete error message. Then the readers do not have to guess, in which line the error occurs.
input=[input ,avrgVolt ] % Here
dataPlot = [voltageS1, voltageS2, avrgVolt]; % or here?

Sign in to comment.

Answers (1)

Jan
Jan on 3 Jul 2022
The message means, that you cannot concatente arrays horizontally, if they have a different number of rows. Or vertically, if the number of columns differ.
Use the debugger to find the problem. Type in the command window:
dbstop if error
Now run your code again. If Matlab stops at the error, check the dimensions of the arrays to be concatented.

Community Treasure Hunt

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

Start Hunting!

Translated by