How to rename identical variables under one common name?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rookie Programmer
el 6 de Jul. de 2023
Respondida: Peter Perkins
el 17 de Jul. de 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:
Expected Table Below:
4 comentarios
Matt J
el 6 de Jul. de 2023
It is better to attach your examples as .mat files with actual data variables in them.
Respuesta aceptada
Stephen23
el 7 de Jul. de 2023
Editada: Stephen23
el 7 de Jul. de 2023
T = readtable('myData.xlsx')
T.Type = regexp(T.Type,'^[A-Z]{4}','match','once')
2 comentarios
Jon
el 7 de Jul. de 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
el 7 de Jul. de 2023
Editada: Stephen23
el 8 de Jul. de 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"
Más respuestas (3)
sushma swaraj
el 6 de Jul. de 2023
Editada: sushma swaraj
el 6 de Jul. de 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 comentarios
Jon
el 6 de Jul. de 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 comentarios
Jon
el 7 de Jul. de 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
el 10 de Jul. de 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
el 17 de Jul. de 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 comentarios
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!