Dear Sir/Madam,
I am fairly new to matlab and im having a problem with converting text in my table to different names. I have a table called X and under the column 'StepID' I have repeated characters of 'a to h' (8000 rows). I would like to change 'a' to 'protein A' and 'b' to 'VI' and 'c' to a different a name and so and and so forth until h. How could I implement this in Matlab.
Any help is greatly is greatly appreciated (using R2020b)
Best Regards,
Jeevs S

 Akzeptierte Antwort

dpb
dpb am 14 Mai 2022

0 Stimmen

tSingh=readtable('singhData.xlsx');
catnames={'Protein A','VI','DI','AN', 'UF','CA','VRF','DF'};
tSingh.Step=categorical(tSingh.StepID,unique(tSingh.StepID),catnames);
results in
>> head(tSingh)
ans =
8×3 table
StepID M Step
______ _____ _________
{'a'} 0.00 Protein A
{'b'} 0.00 VI
{'c'} 0.00 DI
{'d'} 0.00 AN
{'e'} 0.00 UF
{'f'} 0.00 CA
{'g'} 10.00 VRF
{'h'} 5.71 DF
>>
You can, of course, replace 'StepID' in place if don't need the original any longer...
NB: the shorthand to get the valueset list instead of the constructed list...it's cleaner and is also sorted by unique()

Weitere Antworten (1)

dpb
dpb am 14 Mai 2022
Bearbeitet: dpb am 14 Mai 2022

0 Stimmen

It's always bestest to attach a short section of your file/variable that illustrates...but will take a stab at it from the description.
Sounds like a place for a categorical variable instead of just string substitution -- try
catnames={'Protein A','VI', ...}; % DEFINE THE display names for each category
tX.Step=categorical(cellstr(tX.Step),cellstr(['a':'h'].'),catnames);
You didn't provide the full list; you'll have to create it to match as desired...

3 Kommentare

gurjeevan singh
gurjeevan singh am 14 Mai 2022
Bearbeitet: gurjeevan singh am 14 Mai 2022
Dear dpb,
Thank you for getting back to me. Apologies for not putting in the full list.
The full list is:
catnames={'Protein A','VI','DI','AN', 'UF','CA','VRF','DF'};
I have now attatched the table is to the original question post.
I have tried you solution, however, Im getting the following error; is there something Im doing wrong ?
I have called my table massloss, which shows in the below error.
massloss.StepID=categorical(cellstr(massloss.StepID),>> cellstr(['a':'h'].',catnames);
Error: Invalid use of operator.
Thank You,
Jeevs S
dpb
dpb am 14 Mai 2022
Bearbeitet: dpb am 14 Mai 2022
That's a fignewton of the cut' n paste from the command line I didn't see...the ">>" command prompt doesn't belong in there; not sure how it did get there, but..
catnames={'Protein A','VI', ...}; % DEFINE THE display names for each category
tX.Step=categorical(cellstr(tX.Step),cellstr(['a':'h'].'),catnames);
will have a much better chance....
gurjeevan singh
gurjeevan singh am 14 Mai 2022
Bearbeitet: gurjeevan singh am 14 Mai 2022
Thank you SO much ! It worked ! yay! saviour !
Best, Jeevs S

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by