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 Center および File ExchangeText Data Preparation についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by