How to erase a specific elements for the name of string values and then replace them?

Hello everyone,
I have names of values in this form:
dd_mm_yy_hh_mm_ss
for example:
Temp_06_02_24_10_39_34 = 32;
I would like to select only the number for the month (02) and replace it with the coresponding name of month in order to creat new value, for example:
Temp_Feb = 32;
How can I do this ?
Best regards, Ahmad

3 Kommentare

Is "Temp_06_02_24_10_39_34 " a variable name or is "Temp_06_02_24_10_39_34 = 32;" a string value?
Very bad data design has meta-data (e.g. dates) in variable names. Such badly-designed data will force you into writing slow, complex, inefficient, obfuscated code that is fragile/buggy and hard to debug. Best avoided:
Learn to use arrays.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Assumptions:
  • "Temp_06_02_24_10_39_34 = 32"; is a string
  • This is not a variable name
  • The month is in the 2nd numeric position (02, above)
  • The goal is to replace the entire day_month_year_hour_minute_second datetime with the 3-letter abbreviation for the month.
str = "Temp_06_02_24_10_39_34 = 32;";
% Extract the month number and convert to short month name
[~,dtParts] = regexp(str, '_(\d+)','match','tokens');
monthStr = month(datetime(str2double([dtParts{:}])),'shortname');
% Replace the datetime portion with the short month name
newStr = regexprep(str, '(_\d+){6}',['_',monthStr{1}])
newStr = "Temp_Feb = 32;"

4 Kommentare

Thanks alot. This is what I want.
Here's a combination of my solution and @Jon's neat idea to specify datetime format. This avoids using tokens.
str = "Temp_06_02_24_10_39_34 = 32;";
monthStr = month(datetime(regexp(str,'\d+_\d+_\d+','match','once'),'InputFormat','dd_MM_yy'),'shortname');
newStr = regexprep(str, '(_\d+){6}',['_',monthStr{1}])
newStr = "Temp_Feb = 32;"
Here is an approach using a dynamic regular expression. This avoids the extra REGEXP and MONTH function calls.
str = "Temp_06_02_24_10_39_34 = 32;";
fnh = @(s)char(datetime(s,'InputFormat','_dd_MM_yy_hh_mm_ss','Format','MMM'));
new = regexprep(str,'(_\d+){6}','_${fnh($0)}')
new = "Temp_Feb = 32;"
Thank you Stephen. This is also a good approach.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

I would suggest in general that you not embed the date time data in the variable name. Instead use a table, timetable, or some other data structure to hold these values along with the temperatures. Here's a little example using just a MATLAB table
% Make example table to hold data
yyyy = [2023,2023,2023,2024]'
yyyy = 4x1
2023 2023 2023 2024
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mm = [8,4, 2, 3 ]'
mm = 4x1
8 4 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dd = [15,18,6, 2]'
dd = 4x1
15 18 6 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hh = [9,13,8,23]'
hh = 4x1
9 13 8 23
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mi = [15,18,59,42]'
mi = 4x1
15 18 59 42
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
sec = [11,22,19,49]'
sec = 4x1
11 22 19 49
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
T = [29,15.3,22, 36.4]'
T = 4x1
29.0000 15.3000 22.0000 36.4000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
time = datetime(yyyy,mm,dd,hh,mi,sec);
Tdata = table(time,T)
Tdata = 4x2 table
time T ____________________ ____ 15-Aug-2023 09:15:11 29 18-Apr-2023 13:18:22 15.3 06-Feb-2023 08:59:19 22 02-Mar-2024 23:42:49 36.4
% Find the temperature for February (month = 2)
idl = month(Tdata.time) == 2;
Tdata.T(idl)
ans = 22

9 Kommentare

I just want to change the name. How can I do this?
I don't know what you mean, that you just want to change the name. In anycase you are really heading in the wrong direction embedding the date and time data in the variable names. It is very difficult, and you miss harnessing the whole power of MATLAB if you go that way.
changing the name of string value to create new name
Sorry, I'm really not understanding what you are trying to do. I reread your question but I still don't get it. Please either try again to give a very simple, example of exactly what you want to do, or wait, and maybe someone else understands what you are asking and they can help you
"I have names of values in this form"
The most important question is: how did you get all of these variables into the workspace? That is one opportunity to fix this bad data design. For example, if you used LOAD then LOAD into an output variable and access its fields:
S = load(..)
@Jon it is okay.
load data
info = data.fileinfo.Temp_time; % info = Temp_06_02_24_10_39_34
I just want to use this string value to name a new string value (Temp_Feb).
If the value is Temp_15_05_24_10_44_22 I changed it to Temp_May
It looks like the goal is to use Temp_15_05_24_10_44_22 or Temp_May as a variable name. Don't do this. It's really bad practice. If the month is meaningful, store it somewhere else in another variable or with in a table that contains all of the data so the data travels together.
Here is an approach for extracting the month from a string with day month year ..and then creating a new string with just the month
s1 = 'Temp_15_05_24_10_44_22'
s1 = 'Temp_15_05_24_10_44_22'
time = datetime(s1(6:end),'InputFormat','dd_MM_yy_HH_mm_ss')
time = datetime
15-May-2024 10:44:22
mname = month(time,"shortname");
s2 = "Temp_" + mname{1}
s2 = "Temp_May"
@Jon Thanks a lot. This solution is easier and better.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by