Change Name of String in Cell Array if Repeated More Than Once...
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Good afternoon!
I'm currently working with hundreds of files that each represent an individual day. Every time an event occurs, I identify the date on which it occurred and add the date (i.e. a string with the date) to a cell array that lists all of the dates from my study period when the event occurred. Ultimately, this cell array helps me when its time to move all of my selected .nc4 files from one big folder containing every day in my study period to a smaller folder with only files for days when the event occured. However, sometimes an event occurs more than once in one day, in which case I have to add the string with the date multiple times into my cell array as well as copy the same file multiple times into my small folder. For example, the file for December 25th, 1990 originally looks something like this:
'wind_file_12_25_1990.nc4'
Although the name of my string in the cell array does not change (meaning it just repeats 'wind_file_12_25_1990.nc4' four times, when I copy the file three more times into the same folder (assuming the event occurred four times that day and I need four files), I get copies of that file that have names like the following:
'wind_file_12_25_1990.copy1.nc4'
'wind_file_12_25_1990.copy2.nc4'
'wind_file_12_25_1990.copy3.nc4'
This is expected since each file in a folder must have a unique name.
That being said, I would like to put together an efficient code that identifies the number of times an event occurred on a particular day. That way, when an event occurs four times (i.e. when one date appears four times in my cell array), I can have an original file with the original name ('...1990.nc4') as well as copies of that file that end in 'copy1.nc4' and 'copy2.nc4' and 'copy3.nc4'. Please note that an event can also occur two times in one day, in which I would only need the original file and one copy, or 'copy1.nc4'.
Here's what I have so far...
selected_files
[xx, yy, zz] = unique(my_files, 'rows')
x = find(hist(zz, unique(zz))==1)
once = []
for i = 1:length(zz)
zz(i)
for j = 1:length(x)
x(j);
if zz(i)==x(j)
ind = find(zz==x(j));
once = [once; selected_files(ind,:)];
end
end
end
Here, I have used the "unique" function to try to see how many times a string repeats itself. In the case above, I see which strings have no repetitions, or where my event occurs only once a day, and create an array with the strings of dates where my event occurred only once. (I don't have to add anything here since I don't have to create a copy of the file. I would like to do this for when the string repeats once (event occurs twice a day) and when it repeats twice (event occurs three times a day) and also when it repeats three times (event occurs four times in one day).
Any suggestions would be greatly appreciated! Thanks in advance. :)
2 Kommentare
Chris
am 1 Nov. 2021
Could you provide a bit of code or a .mat file that recreates a small my_files array, so we can be more certain of how it's organized?
Jan
am 1 Nov. 2021
I've read the question 3 times and cannot find out, what exactly the inputs are and what you want to achieve. What is the purpose of the posted code? Does it work as expected? What is missing?
Do you search a method to count the number of repetions in a cell string? Are you asking for a method to create the file names?
Antworten (1)
Jan
am 1 Nov. 2021
Do I understand correctly, that your question is:
I have a cell string. How do I count the number of repetions of each char vector in it?
Does this matchs your needs? If so, my formulation is more compact.
Then:
data = {'A', 'B', 'A', 'C', 'D', 'A'};
[N, S] = CountElem(data)
function [N, S] = CountElem(A)
nA = numel(A);
S = sort(A(:).');
q = [true, ~strcmp(S(1:nA - 1), S(2:nA))];
N = diff([find(q), nA + 1]);
S = S(q);
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Dates and Time 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!