How to reform cell into a vector of strings and text

조회 수: 1 (최근 30일)
Lucas Maes
Lucas Maes 2021년 10월 21일
댓글: Lucas Maes 2021년 10월 22일
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