Character string to numeric vector
3 次查看(过去 30 天)
显示 更早的评论
Dear all
I am extracting from a file in a for loop information in the following form: ' ( 0.0357 -0.0001 0.0051) ', which is a 1x27 char. I would like to store the three displayed numbers in a the three components of an array so I can use these figures to do mathematical operations. How could I transform them to do so in this case, keeping the minus signs that can appear for different steps of the for loop too?
回答(2 个)
Sameer
2024-9-20
Hi Richard,
To extract the numbers from the string and store them in an array, you can use the "sscanf" function
Here's how you can do it:
% Example string
str = ' ( 0.0357 -0.0001 0.0051) ';
% Use sscanf to extract the numbers
numbers = sscanf(str, ' ( %f %f %f) ');
disp(numbers);
Please refer to the below MathWorks documentation link:
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!