Saving a variable to a continuously changing file name

I would like to save a variable to a file/filename that relates to a previously loaded filename and repeat the code with a new file and save the variable to a different filename.
First I load in a file of choice
data = uigetfile('*.csv');
IP_Testing = xlsread(data)
example: BurnIn_2ndary_volt_curr_columnar_20150527_1940_IAM002.csv
Note: The datetime is in the filename
Then I reduce the file to a small matrix from data that I selected with the brushing tool. I assign that matrix to a variable.
details = [mean(IAMHS(:,2)) std(IAMHS(:,2)) min(IAMHS(:,2)) max(IAMHS(:,2));...
mean(CMP1(:,2)) std(CMP1(:,2)) min(CMP1(:,2)) max(CMP1(:,2));...
mean(CMP2(:,2)) std(CMP2(:,2)) min(CMP2(:,2)) max(CMP2(:,2));...
mean(PDU_OP(:,2)) std(PDU_OP(:,2)) min(PDU_OP(:,2)) max(PDU_OP(:,2));...
mean(ThreePThreeV(:,2)) std(ThreePThreeV(:,2)) min(ThreePThreeV(:,2)) max(ThreePThreeV(:,2));...
mean(TenV_PREF(:,2)) std(TenV_PREF(:,2)) min(TenV_PREF(:,2)) max(TenV_PREF(:,2));...
mean(TenV_REF(:,2)) std(TenV_REF(:,2)) min(TenV_REF(:,2)) max(TenV_REF(:,2))];
Now I would like to save the matrix (details) to a file name that relates to the previous loaded file (same datetime).
Examples: BurnIn_2ndary_20150527_1940.mat
-------------------------------------------------------------------------------------------------------------
The problem is I will run my code to retrieve another file similar to the first one I imported (different datetime).
Example: BurnIn_2ndary_volt_curr_columnar_20150528_1500_IAM002.csv
In the end I would like to save it to a similar name
Examples: BurnIn_2ndary_20150528_1500.mat
So I need my code that saves the file to be able to recognize a different file so that I can save it to a different filename. Being able to hold on to the datetime in the name is important for later plotting all of the new files I have saved

 采纳的回答

You can use regexp() to extract portions of the name. For example,
regexp(filename, '\d+_\d+', 'match')
should extract the data and time substring.
The first part looks to be
regexp(filename,'^[A-Za-z0-9]+_[A-Za-z0-9]+', 'match')

11 个评论

When I run that code for my file it outputs
'BurnIn_2ndary'
Is there a way to skip volt, curr, and columnar in the filename
'BurnIn_2ndary_volt_curr_columnar_20150530_2200_IAM002.csv'
and continue to retrieve the numbers for the datetime?
I tried
regexp(data,'^[A-Za-z0-9]+_[A-Za-z0-9]+_[0-9]+_[0-9]+_[0-9]+_[0-9]+_[0-9]+', 'match')
First time using the command but I thought maybe searching for numbers in the names before the datetime would exclude them with that. When I ran it, I only got
{ }
The easiest thing is to use both calls that I show and put them together.
If you really want to do it in one unreadable call, then
regexprep(filename,{'(?<=[A-Za-z0-9]+_[A-Za-z0-9]+_).*?(?=\d+_\d+_)','_[A-Za-z0-9]+\.csv$'}, '')
I see, I didn't realize the first line of code you shared was specific for datetime. I thought it was just an example.
Now how would I save this details matrix with the preferred filename extracted (substring_datetime or BurnIn_2ndary_20150528_1500) with my details matrix? I can't have the code using one simple name because each time I run a new set of data it will need to save to a different filename
first_part = regexp(filename,'^[A-Za-z0-9]+_[A-Za-z0-9]+_', 'match');
second_part = regexp(filename, '\d+_\d+', 'match')
preferred_filename = [first_part second_part];
save(preferred_filename, 'details')
I had tried something similar to that and that but then I got an error each time with save
(argument must contain a string)
Perhaps you left out the quotes around 'details'
No they are there.. It seems like it cannot be done without the preferred_filename being in quotes which wouldn't produce the right filename of course. I would like to save it as a .mat file too. What do you think?
Error using save
Argument must contain a string.
Error in data_2ndary (line 96)
save(preferred_filename, 'details')
Walter, it seems the issue occurs because when done separately the preferred file name is a two cell matrix and cannot be a string. However using the longer code below that is a little confusing to me works by assigning it to variable, a, and then saving. Weird but I'll take it
a = regexprep(filename,{'(?<=[A-Za-z0-9]+_[A-Za-z0-9]+_).*?(?=\d+_\d+_)','_[A-Za-z0-9]+\.csv$'}, '');
save(preferred_filename, 'details')
The only issue is it outputs: BurnIn_2ndary_0150601_1600
It is missing the 2 in 20150601.
Should be: BurnIn_2ndary_20150601_1600
I was able to fix the issue by playing around and adding an underscore right before the datetime export
a = regexprep(filename,{'(?<=[A-Za-z0-9]+_[A-Za-z0-9]+_).*?_(?=\d+_\d+_)','_[A-Za-z0-9]+\.csv$'}, '');
If you don't mind explaining the unreadable call too that would be greatly appreciated. Thank you for all of your help!
preferred_filename = [first_part{1} second_part{1}];
Simplified version that does it all in one, producing a character string:
regexprep(filename,{'(?<=^[^_]+_[^_]+_)[^0-9]+','_[^_]+\.csv$'}, '')
Walter could you give me an example on how to extract different parts of this filename?
BurnIn_2ndary_volt_curr_columnar_20150528_1500_IAM002.csv
I also have a range3 (which is a character string that is equal to 2324_0226) that I would like to tack onto it. Examples of what I would like to extract from the original are below.
BurnIn_2ndary_volt_curr_20150528_IAM002_
BurnIn_2ndary_20150528_IAM002_
BurnIn_2ndary_20150528_
Now I would like to combine the existing string range3 to produce the following.
BurnIn_2ndary_volt_curr_20150528_range3_IAM002 or
BurnIn_2ndary_volt_curr_20150528_2324_0226_IAM002
BurnIn_2ndary_volt_curr_20150528_IAM002_2324_0226
BurnIn_2ndary_20150528_IAM002_2324_0226
BurnIn_2ndary_20150528_2324_0226

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by