Why am I getting "Subscripted assignment dimension mismatch" for this code

5 ビュー (過去 30 日間)
Adam
Adam 2014 年 7 月 15 日
編集済み: Geoff Hayes 2014 年 7 月 15 日
val = zeros(10000,1);
for i=1:10000;
val(i,1)=fscanf(a.aser,'%d');
end
This code first creates and empty matrix and then replaces each element of the matrix with an analogue output voltage value of an Arduino board.
I'm getting the "dimension mismatch" error for the third line of the code. Can somebody help me?

回答 (2 件)

José-Luis
José-Luis 2014 年 7 月 15 日
Might be a few things. Maybe at some point fscanf is returning nothing and you are trying to assign an empty array. Without looking at the files, it is hard to say.
  2 件のコメント
Ben11
Ben11 2014 年 7 月 15 日
maybe there is an index missing, for example using a.aser(i) might work? As Jose-Luis said it's hard to tell without more infos.
José-Luis
José-Luis 2014 年 7 月 15 日
a.aser has to be a fileID, though.

サインインしてコメントする。


Geoff Hayes
Geoff Hayes 2014 年 7 月 15 日
編集済み: Geoff Hayes 2014 年 7 月 15 日
The val matrix has 10000 rows and one column. I suspect that at the third line of code, more than one value is being read from a.aser. To verify this, on each iteration of the loop, do the following
[data,count] = fscanf(a.aser,'%d');
fprintf('count=%d\n',count);
If the output of count is more than one, then that would explain the dimension mismatch error. What you can probably do is read only one integer at each iteration
for i=1:10000;
[data,count] = fscanf(a.aser,'%d',1);
if count==1
val(i,1) = data;
else
break;
end
end
An alternative would be to just use [data] = fscanf(a.aser,'%d'); to grab as much data available as possible.
Try the above and see what happens!

カテゴリ

Help Center および File ExchangeArduino Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by