Hello everyone
I'm trying to separate strings from a datatable, of one specific column that contains this for instance:
2019-04-22 Clean A8
and I want to separate date from the strings.
I used this commands:
time=Clean(:,1);
str=string(time);
tiempo=strsplit(str);
But when I apply the last one appears this error:
First input must be either a character vector or a string scalar.
Which I don´t get it, because there are string in there? Aren´t they?
Do I have to work with datatable or cells?
Please help

1 Kommentar

Peter Perkins
Peter Perkins am 12 Jun. 2019
I would think the more fundamental question is how you got into this situation to begin with. Are you reading a file? If so can you use detectimportoptions and then readtable to read the data in correctly?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Jun. 2019

1 Stimme

tiempo = arrayfun(@strsplit, str, 'uniform', 0);

8 Kommentare

When I apply the last command, I get a 298 x 1 cell array, and all says:
tiempo = arrayfun(@strsplit, str, 'uniform', 0)
tiempo =
298×1 cell array
{1×3 string}
{1×3 string}
{1×3 string}
{1×3 string}
{1×3 string}
how can I see, for instance, only the date 2019-04-22?
these are the commands I wrote:
time=Clean(:,1);
str=string(time);
tiempo = arrayfun(@strsplit, str, 'uniform', 0);
Jonathan Bijman
Jonathan Bijman am 12 Jun. 2019
Thanks Walter! I already realize that it works.
Thank u so much =)
From the last command
tiempo = arrayfun(@strsplit, str, 'uniform', 0);
I get a cell with 1 x 3 strings. But how can I extract only the date from there?
arrayfun(@(V) V{1}, tiempo, 'uniform', 0)
I wrote this
time=Clean(:,1);
r=string(time);
tiempo = arrayfun(@strsplit, r, 'uniform', 0);
tiempo_1 =arrayfun(@(V) V{1}, tiempo, 'uniform', 0)
And appears the same thing:
1 x 3 string
And when I click on it, appers the same as the picture.
The idea is the date appears as one column separate from the other ones, as a table or array (I dunno what's better)
pic.jpg
"I get a cell with 1 x 3 strings. But how can I extract only the date from there? "
Look at the content of the cell array...as you asked to split on delimiters and your input array consists of three substrings, (surprise) strplit returned each substring in each row of a three-column cell array.
Hence, the first column contains the date, the second two the subsequent string data fields.
tiempo{:,1}
contains the time string.
cellfun(@(V) V{1}, tiempo, 'uniform', 0)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 12 Jun. 2019

1 Stimme

As the error message says, strsplit only operates on the strings class or char arrays--it can't handle either cell strings (the reason for which I do not understand) or the result of a table operation that returns a table...
>> Clean={'2019-04-22 Clean A8'}; % sample data
>> Clean=cell2table(Clean) % convert to table (if that is what mean with datatable; we're guessing)
Clean =
table
Clean
_____________________
'2019-04-22 Clean A8'
>> Clean(:,1) % address the variable with numeric subscripting--returns another table
ans =
table
Clean
_____________________
'2019-04-22 Clean A8'
>> strsplit(Clean(:,1)) % and strsplit can't deal with that...as you already found out
Error using strsplit (line 80)
First input must be either a character vector or a string scalar.
>> Clean{:,1} % dereference with curlies {} to get content instead of table--
ans =
1×1 cell array
{'2019-04-22 Clean A8'}
>> strsplit(Clean{:,1}) % but, as error says it can't handle cellstr, either... :(
Error using strsplit (line 80)t
First input must be either a character vector or a string scalar.
>> strsplit(char(Clean{:,1})) % but it can handle a char() array as the message says
ans =
1×3 cell array
{'2019-04-22'} {'Clean'} {'A8'}
>> strsplit(string(Clean{:,1})) % or a string...
ans =
1×3 string array
"2019-04-22" "Clean" "A8"
>> strsplit(string(Clean(:,1))) % but notice you have to dereference the content or...
Error using string
Conversion to string from table is not possible.
>>

4 Kommentare

I put these commands:
Clean_1=cell2table(Clean);
tiempo=Clean_1(:,1);
tiempo=Clean_1{:,1};
strsplit(string(tiempo{:,1}));
and appears this error:
Error using string
No constructor 'string' with matching signature found.
Error in Prueba_1 (line 29)
strsplit(string(tiempo{:,1}));
If I turned data into a table, why does appear this error?
I didn´t say it, but it's a lot of data with the same structure
date Clean Letter
Well, dunno...
What does
which -all string
return?
Also, it still isn't absolutely clear what you started with--I made some assumptions that your original data was in a cellstr based on the error and that you were using a table because you used the word "datatable" but whether that means a MATLAB table class variable or not is not totally clear.
Now, above, it does appear to have started with a cell array and turned it to a table. The second assignment to the variable tiempo negates the first entirely--there's no point in it.
If I reproduce that with the table I created here, it works just fine...which Matlab release are you using? string is a relatively recent introduction.
Also, post what
whos Clean*
returns as well as
whos tiempo
after the second definition.
Stephen23
Stephen23 am 12 Jun. 2019
@Jonathan Bijman: please upload the variable tiempo in a .mat file by clicking the paperclip button.
Jonathan Bijman
Jonathan Bijman am 12 Jun. 2019
@Stephen Cobeldick

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by