How can I replace numbers in a vector by text ?
Afficher commentaires plus anciens
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'
Réponses (1)
dpb
le 1 Juin 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 commentaires
Diego
le 1 Juin 2014
Diego
le 1 Juin 2014
dpb
le 1 Juin 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
le 2 Juin 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
le 2 Juin 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
le 2 Juin 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
le 2 Juin 2014
Oh. OK. Thought I’d been behind the door again.
Know about ‘table’ but haven’t used it much yet.
Diego
le 2 Juin 2014
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!