Need help with storing live data from sensors into array

10 ビュー (過去 30 日間)
Samuel Louise
Samuel Louise 2018 年 12 月 29 日
コメント済み: Samuel Louise 2019 年 2 月 4 日
this is my code. I want to store the incoming data into my array/matrix whichever would do the trick. This keeps overwriting the previous data into the array. I want the data to be added up at the bottom.
I am also getting this problem. "Conversion to cell from uint16 is not possible."
iterate = 0;
while 1
iterate =plus(iterate, 1); %counter
disp(iterate);
Gyro_X = readRegister(MPU9250, GYRO_XOUT_H, 'uint16')
Gyro_Y = readRegister(MPU9250, GYRO_YOUT_H, 'uint16')
Gyro_Z = readRegister(MPU9250, GYRO_ZOUT_H, 'uint16')
pause(1/10);
%Gyro = table(iterate, Gyro_X, Gyro_Y, Gyro_Z);
Gyro = {};
Gyro(i) = [ iterate, Gyro_X, Gyro_Y, Gyro_Z];
end

採用された回答

Mustafa Abu-Mallouh
Mustafa Abu-Mallouh 2018 年 12 月 30 日
編集済み: Mustafa Abu-Mallouh 2018 年 12 月 30 日
If you want to store the values in each row, call out that you want the values to take over all columns of "Gyro". See below:
i = 0;
max_data_len = 10;
% Initialize 'Gyro' with desired dimensions
% - Don't have to but it helps performance
Gyro = zeros( max_data_len , 4 );
while i < max_data_len
i = i+1;
Gyro_X = randi(30,1);
Gyro_Y = randi(30,1);
Gyro_Z = randi(30,1);
% If creating a results array
Gyro(i,:) = [i Gyro_X Gyro_Y Gyro_Z];
end
Another issue is you redefine "Gyro" as an empty cell array every iteration of the loop when you call the following code:
Gyro = {};
This clears the data you originally had stored in the variable. This is also the reason you are getting the "Conversion to cell from uint16 is not possible" error; it is trying to convert your 16 bit integer array into a cell array
  2 件のコメント
Samuel Louise
Samuel Louise 2018 年 12 月 31 日
Thank you for your help! Much appreciated.
Samuel Louise
Samuel Louise 2019 年 2 月 4 日
Hi,
I am having another problem and it is to do with the time it is taking for MATLAB to take the data from the FIFO. Matlab is processing it in seconds. For example
while i <= 50
i = i+1;
Gyro_X = randi(30,1);
Gyro_Y = randi(30,1);
Gyro_Z = randi(30,1);
% If creating a results array
Gyro(i,:) = [i Gyro_X Gyro_Y Gyro_Z];
end
the problem with 'i' is that 'i' is being read in 50 seconds. Is there anyway i can speed up that process?
Thank you for your help.

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

その他の回答 (1 件)

ahmed nebli
ahmed nebli 2018 年 12 月 29 日
編集済み: madhan ravi 2018 年 12 月 29 日
what you need to do is create a vector/matri without defining its size first (e.g.: A=[]; ) then use the command vertcat.
  1 件のコメント
Samuel Louise
Samuel Louise 2018 年 12 月 29 日
Hi,
Thank you for the prompt reply. But I have tried to follow what your instructing but it is constantly replacing the old value to the new value in the loop.
I am now getting this error : The RowNames property must be a cell array, with each element containing one nonempty character vector.
After doing this:
Gyro = table([Gyro_X],[Gyro_Y],[Gyro_Z],'VariableNames',{'GYRO_X','GYRO_Y', 'GYRO_Z'},'RowNames',{iterate})

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

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by