How can I replace numbers in a vector by text ?
Show older comments
Hi everybody!
I have a column in a csv file with 0 and 1 and I want to replace all the 0 values by FALSE and all the 1 values by TRUE. Anybody knows how can I do that? Thanks in advance!
I have been trying to do this but it doesn't work.
s(s==0)='FALSE'
Answers (1)
dpb
on 1 Jun 2014
s(s==0)='text';
won't work because you're trying to change datatypes piecewise in an array.
SOTOO,
>> s=rand(10,1)>0.5;
>> v={'t';'f'};t=v(s+1)
t =
't'
'f'
't'
't'
'f'
'f'
'f'
't'
'f'
't'
>>
8 Comments
Diego
on 1 Jun 2014
Diego
on 1 Jun 2014
dpb
on 1 Jun 2014
Say What????
You can't hold character and numeric data in the same array...if it's a cell array, it'll have to hold a column that's char and another that is numeric in the cell array as two cells.
Show in a sample code snippet what you're trying to do precisely, but sounds like it's an attempt at the impossible. Oh, there's the new datafun object that can mix metaphors if you've got a very recent version. Maybe that'd solve the problem...
Star Strider
on 2 Jun 2014
Fascinated by the possibility, I did a search of the online documentation and couldn’t find any ‘datafun’ object. There’s a datafun directory but nothing else.
Where didn’t I look?
Image Analyst
on 2 Jun 2014
Not sure I understand what dpb's saying. You can hold numbers and strings in a cell array and they don't need to be all the same across rows or columns. Here's proof:
a{1,1} = 1;
a{2,1} = 'Hello World!';
a{1,2} = 'MATLAB';
a{2,2} = 22
I think he was referring to a "table" which is a new object (rather than datafun), and for a table, everything in a column must be the same data type (all numbers or all strings).
dpb
on 2 Jun 2014
That's what I was at least trying to say IA; you can put them in a mixed cell, but as different cell elements, not within a single cell--that doesn't work.
And, yes, I misspoke on the meaning the new table -- I don't have it so wasn't sure of it's facilities.
Star Strider
on 2 Jun 2014
Oh. OK. Thought I’d been behind the door again.
Know about ‘table’ but haven’t used it much yet.
Diego
on 2 Jun 2014
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!