How to rename identical variables under one common name?
Ältere Kommentare anzeigen
I've read in a Excel file through MATLAB that is a 1191x12 Table.
The goals is to rename identical variables to one common variable name, Example below:
For any type that start with ABCD, Id like to rename to only ABCD removing the following characters/numbers.
For any type that starts with BACA, Id like to rename to only BACA removing the following characters/numbers.
For any type that starts with CABD, Id like to rename to only CABD removing the following characters/numbers.
For any type that starts with DABC, Id like to rename to only CABD removing the following characters/numbers.
Current Table Example below:

Expected Table Below:

4 Kommentare
Not enough information: do you want to split/delete those characters based on their content/character type (e.g. digits) or based on their location in the text (e.g. indices >4) or based on some other condition (e.g. what?)...?
Do you know in advance the "common names", or do you want to automatically detect the "common names" ?
Please upload your data file by clicking the paperclip button.
Dyuman Joshi
am 6 Jul. 2023
If all the data is 4 letters followed by 3 digits, simply remove the digits by indexing or store the 1st 4 values only.
Matt J
am 6 Jul. 2023
It is better to attach your examples as .mat files with actual data variables in them.
Rookie Programmer
am 6 Jul. 2023
Akzeptierte Antwort
Weitere Antworten (3)
sushma swaraj
am 6 Jul. 2023
Bearbeitet: sushma swaraj
am 6 Jul. 2023
0 Stimmen
Hi, You can use the 'startsWith' function instead of 'contains' to get the appropriate result.
Hope it helps you in modifying your code!
Jon
am 6 Jul. 2023
Here's a general way to handle this. Note no need for all those if statements
% Read in the data
T = readtable('myData.xlsx'); % put the name of your data file here
% Strip off the numeric part of the data in the 'Type' column
T.Type = cellfun(@(x) x(~isstrprop(x,'digit')),T.Type,'UniformOutput',false)
% Save the data back into a new Excel file (not sure if you need to do
% this)
writetable(T,'myData_stripped.xlsx')
3 Kommentare
Explaining a little further
The function isstrprop(x,'digit') returns a logical vector of 1's and zeros with a 1 (true) wherever x contains a numeric digit
isstrprop('ABCD123','digit')
So notting this, e.g. ~isstrprop('ABCD123','digit') will give you a true wherever the name has a nonnumeric letter
~isstrprop('ABCD123','digit')
If we just want to keep the non-numeric elements in the string we can use logical indexing to select them
name = 'ABCD123'
newName = name(~isstrprop(name,'digit'))
In your case you want to do this for a whole column in a table. The column in the table is actually a m by 1 cell array of values, that would look something like this
names = {'ABCD123';'BACCA654';'CABD154'}
To do something to every element in a cell array you use cellfun, so putting it all together using the anonymous function @(x) x(~isstrprop(x,'digit')) which says what to do with each element x
newNames = cellfun(@(x) x(~isstrprop(x,'digit')),names,'UniformOutput',false)
You can read more about anonymous functions here https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html
and about cellfun here https://www.mathworks.com/help/matlab/ref/cellfun.html
As noted in my comment to @Stephen23
If you already know that you just want the first 4 characters, no need to use regexp, or cellfun, just use extractBetween
T = readtable('myData.xlsx')
T.Type = extractBetween(T.Type,1,4)
Jon
am 10 Jul. 2023
Did any of our responses answer your question? If so please accept an answer so that others will know an answer is available. If not please let us know what aspect of your problem we are missing.
This just SCREAMS for using categorical. Screams. This might seem like more work, but untimately you will be happier.
TextData = ["aa";"bb";"cc";"aa";"bb";"cc";"aa";"bb";"cc";"aa"] + randi([100 200],10,1);
t = table(rand(10,1),TextData)
t.CatData = categorical(t.TextData)
oldCats = string(categories(t.CatData));
newCats = unique(extractBetween(oldCats,1,2))
for i = 1:length(newCats)
toBeMerged = startsWith(oldCats,newCats(i));
t.CatData = mergecats(t.CatData,oldCats(toBeMerged),newCats(i));
end
t.TextData = []
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!