How to reform cell into a vector of strings and text

1 次查看(过去 30 天)
Hi, I want to read in a excel with the first cell of each row of the form:
2020-09-02 00:00:00.7770000,"OLW218","Pickling","Water lock Off-time PV (Sec)",105,,,"Good"
(this is all in one cell)
and replace it with a vector of the form
[0.777,"OLW218","Pickling","Water lock Off-time PV (Sec)",105,"Good"]
or a form where I can for example have: vector(6) = "Good".
If I use xlsread or save the excel as txt file it is imported as a cell {"2020-09-02 00:00:00.7770000,"OLW218","Pickling","Water lock Off-time PV (Sec)",105,,,"Good""}. I tried the split command, but I can't get the result I want.
  2 个评论
dpb
dpb 2021-10-21
Why don't you use the Excel whizard and convert/split the text there, just out of curiosity?
How did the file get created in this manner in the first place?
The easiest problem to solve is the one that don't create in the first place...going back to the beginning might be the simplest fix.
Lucas Maes
Lucas Maes 2021-10-22
Hi dpb
That is an option, but there are a lot of files and I don't want to change them all manually. Creating them differently might be the easiest option, but I don't control that haha.

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2021-10-21
Two of many ways to deal with the cellstr --
As string array
>> S=string(split(C,',')); S=S(S~="")
S =
6×1 string array
"2020-09-02 00:00:00.7770000"
""OLW218""
""Pickling""
""Water lock Off-time PV (Sec)""
"105"
""Good""
>>
or as cellstr
>> CC(~cellfun(@isempty,CC))
ans =
6×1 cell array
{'2020-09-02 00:00:00.7770000' }
{'"OLW218"' }
{'"Pickling"' }
{'"Water lock Off-time PV (Sec)"'}
{'105' }
{'"Good"' }
>>
Alternatively, use textscan and convert the fields directly. Will leave as "exercise for student"
  1 个评论
Lucas Maes
Lucas Maes 2021-10-22
Hi dpb,
Thanks for the help. The problem I had with this solution is that ' "Good" ' or ""Good"" cannot be used for example as if S(end) == "Good" ...
The problem actually solved itself by using readtable instead of xlsread.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by