How to rename identical variables under one common name?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rookie Programmer
am 6 Jul. 2023
Beantwortet: Peter Perkins
am 17 Jul. 2023
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1428303/image.png)
Expected Table Below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1428308/image.png)
4 Kommentare
Matt J
am 6 Jul. 2023
It is better to attach your examples as .mat files with actual data variables in them.
Akzeptierte Antwort
Stephen23
am 7 Jul. 2023
Bearbeitet: Stephen23
am 7 Jul. 2023
T = readtable('myData.xlsx')
T.Type = regexp(T.Type,'^[A-Z]{4}','match','once')
2 Kommentare
Jon
am 7 Jul. 2023
Great that regexp operates on whole cell array so no need to use cellfun.
On the other hand you seem to only look at the first 4 elements of the string. Your code won't strip of the numerical part of the strings if there are more than 4 leading characters as shown below for the 3rd row.
T = readtable('myData2.xlsx')
T.Type = regexp(T.Type,'^[A-Z]{4}','match','once')
If you already know that you just want the first 4 characters, no need to use regexp, or cell fun, just use extractBetween
T = readtable('myData.xlsx')
T.Type = extractBetween(T.Type,1,4)
Stephen23
am 7 Jul. 2023
Bearbeitet: Stephen23
am 8 Jul. 2023
"On the other hand you seem to only look at the first 4 elements of the string."
That is exactly why I asked the OP for clarification, what their specific requirements are:
So far the OP has not stated how many leading characters they want to retain, we only have their examples to go by: are they complete and representative? Are they always uppercase? I doubt it... but no one here knows.
"Your code won't strip of the numerical part of the strings if there are more than 4 leading characters as shown below for the 3rd row."
The OP states that they wish to "removing the following characters/numbers", so your assumption that the characters to remove are "numerical parts" seems to be inconsistent with what the OP states (if they only mean "numerical parts" as you wrote what do they mean by "characters"?). But again, this is why I asked for clarification. I see little point in developing and testing regular expressions until I have a reasonably clear specification.
"If you already know that you just want the first 4 characters, no need to use regexp, or cell fun, just use extractBetween"
Weitere Antworten (3)
sushma swaraj
am 6 Jul. 2023
Bearbeitet: sushma swaraj
am 6 Jul. 2023
Hi, You can use the 'startsWith' function instead of 'contains' to get the appropriate result.
Hope it helps you in modifying your code!
0 Kommentare
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
Jon
am 7 Jul. 2023
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.
Peter Perkins
am 17 Jul. 2023
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 = []
0 Kommentare
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!