fscanf - In an assignment A(I) = B, the number of elements in B and I must be the same.

2 ビュー (過去 30 日間)
Darryl
Darryl 2014 年 9 月 23 日
回答済み: dpb 2014 年 9 月 23 日
I cannot get fscanf to work with my Arduino when plotting a graph in real-time. I've read the docs and cannot see where I am going wrong.
I have 4 Arduino Serial.println statements. 3 of which are strings which I don't need. The 4th is an integer which is what I want to plot in real-time.
Here is my code:
ard = serial('/dev/tty.usbserial-A601EQJ5', 'BaudRate', 9600);
fopen(ard);
i = 1;
N = 50;
x = zeros(0, N);
while i <= N
x(i) = fscanf(ard, '%*s %d');
plot(x)
hold on
axis([1, N, 0, 150])
drawnow;
i = i + 1;
end
I keep getting the error that I have written in my title no matter what I change.

回答 (1 件)

dpb
dpb 2014 年 9 月 23 日
x(i) = fscanf(ard, '%*s %d');
x(i) is a single entry; apparently there are either no returned values or more than one reading is available from the fscanf call. Try
x(i) = fscanf(ard, '%*s %d',1);
to read just one return value at a time.

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by