# Replacing Rows or Columns using Indexing (or efficient too solutions) instead of “Cat ”

1 次查看（过去 30 天）
With Arduino, NeoGPS and an MPU6050 i log some data on a SD Card.
On Matlab i am trasforming the accelarations from MPU6050 from the byte values to m/s^2.
1. The code loads the data on Matlab
2. It extracts ax ay az
3. Call a funcion that convert from byte to m/s^2
4. It define the all the columns to be concatenated
5. It concatenates the all the columns
I have a civil engineer back ground, so i am not very practical with coding.
I was wondering if exists a more efficient solution in particular using Indexing?
Here my dumb code
%Open the file
filename= uigetfile ('.csv');
fileID = fopen (filename);
fclose (fileID);
%Converting acceleration from Byte to m/s^2
[ax,ay,az]=convms(logmpu6050);
%Replacing the old accelaration values with the new
cat1=logmpu6050(:,1:8);
cat2=cat(2,ax,ay,az);
cat3=logmpu6050(:,13:15);
newlogmpu6050= cat(2,cat1,cat2,cat3);

### 采纳的回答

Guillaume 2017-5-17

Without changing convms, this is the best you can do:
%Open the file
filename= uigetfile ('.csv');
fileID = fopen (filename);
fclose (fileID);
%Converting acceleration from Byte to m/s^2
newlogmpu6050 = logmpu6050; %copy
[ax,ay,az]=convms(newlogmpu6050);
newlogmpu6050(:, 9:11) = [ax, ay, az]; %replace column 9 to 11 by ax, ay, az
newlogmpu6050(:, 12) = []; %delete column 12
which will give you the exact same result as your current code (assuming that there's no more than 15 columns in logmpu6050).
You could modify convms so that it does direct replacement of columns 9:11 which would be slightly faster.

### 更多回答（2 个）

Jan 2017-5-17
You code looks fine already. I assume reading the CSV file will take much more time. Guillaume's suggestion is a little bit nicer, perhaps it runs some milliseconds faster.
If you do not need the original value of logmpu6050, overwriting this array instead of creating newlogmpu6050 might be a further step.
##### 0 个评论显示 -2更早的评论隐藏 -2更早的评论

Andrea Ciufo 2017-5-24
Thanks! Useful information! :)

### 类别

Help CenterFile Exchange 中查找有关 MATLAB 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