简体   繁体   中英

Manipulating imported string array data set in Matlab

I imported a data set of values from excel into matlab as a string array and called it Pract, this is a 23x2 string. I changed the numbers to the double class type. I am now trying to find a way to loop through my second column of Pract and multiply each number by 4, then store these new values in the next column in the string array by the imported numbers. If anyone is able to help me do this I would really appreciate it, thanks!

Code:

velocity = Pract{:,2} ;

velocity1 = str2double(velocity);

height = Pract{:,1} ;

height1 = str2double(height);

for i = 1:length(velocity)

velocitynew = velocity1*4

end

Using Element-Wise Multiplication and Concatenation

To multiply each element in array velocity you can use element-wise multiplication denoted as .* . The . signifies that you are going to apply this operation to every element of the array which removes the need to use a for-loop. The last line indicates to concatenate the newly calculated column to array Pract .

%Random string array of numbers%
Pract = string(rand(23,2));

height = str2double(Pract(:,1));
velocity = str2double(Pract(:,2));

Pract = [Pract string(velocity.*4)];

Result:

结果

Ran using MATLAB R2019b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM