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

1 ビュー (過去 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})
Thanks for your support.
  1 件のコメント
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 2018 年 9 月 3 日
Hello, you can use textscan() to separate string and numeric vales.
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');
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


Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by