How to reform cell into a vector of strings and text

1 view (last 30 days)
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 Comments
dpb
dpb on 21 Oct 2021
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 on 22 Oct 2021
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.

Sign in to comment.

Answers (1)

dpb
dpb on 21 Oct 2021
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 Comment
Lucas Maes
Lucas Maes on 22 Oct 2021
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.

Sign in to comment.

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!