How to convert a cell array to a double or numeric number and conserve only the needed numeric values?

2 次查看(过去 30 天)
Hello together,
i please need your support concerning one problem, that i face to since 3 hours already :-(. It concern a sensor data processing. My cell look like:
_ *contenuText = 4×3 cell array
{'SensorName'} {'Temperature'} {'Pressure' }
{'Sensor1' } {'temp 40' } {'500.000mBar'}
{'Sensor1' } {'temp 40' } {'500.000mBar'}
{'Sensor1' } {'temp 40' } {'250.000mBar'}*_
for k = 1:length(Pression)
tmpStr = strsplit( Pression{k}, {':','.'} )
tmpStrT = strsplit( Temperature{k}, {':',' '} )
if ~strcmp( tmpStr{k},'')
tmp = str2num(separationStr{k})
end
end
Thanks for your support.
  1 个评论
dpb
dpb 2018-8-12
Where do these data come from? If the original data are from a file, could read the file directly; if it's from a serial interface or some other form, knowing that would help, too.
The better solution may be to get the data into a better form earlier rather than parsing the cell array as you have here (altho that's certinly do-able; may be more trouble than is needed).

请先登录,再进行评论。

回答(1 个)

GK
GK 2018-9-3
Hello, you can use textscan() to separate string and numeric vales.
textscan()
Use '%s' string and '%f' to separate string and numeric values respectively.
Example 1: Read each column of a text file.
Suppose the text file 'mydata.dat' contains the following:
Sally Level1 12.34 45 1.23e10 inf Nan Yes 5.1+3i
Joe Level2 23.54 60 9e19 -inf 0.001 No 2.2-.5i
Bill Level3 34.90 12 2e5 10 100 No 3.1+.1i
Read the file:
fid = fopen('mydata.dat');
C = textscan(fid, '%s%s%f32%d8%u%f%f%s%f');
fclose(fid);
textscan returns a 1-by-9 cell array C with the following cells:
C{1} = {'Sally','Joe','Bill'} %class cell
C{2} = {'Level1'; 'Level2'; 'Level3'} %class cell
C{3} = [12.34;23.54;34.9] %class single
C{4} = [45;60;12] %class int8
C{5} = [4294967295; 4294967295; 200000] %class uint32
C{6} = [Inf;-Inf;10] %class double
C{7} = [NaN;0.001;100] %class double
C{8} = {'Yes','No','No'} %class cell
C{9} = [5.1+3.0i; 2.2-0.5i; 3.1+0.1i] %class double

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by