How do I put all of the strcat outputs in the same cell array (group) without having the code rewrite over the last output over and over again?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I'm trying to create a group of characters that has the month (in 3 letters) and the date. ex) Jan22, Feb23, Mar21
When I run this code, only the last output is saved under FileName. In this case, xxx01 is the last output, so that's what is saved under FileName.
I think the outputs keep on getting written on top of each other. It's my first time working with MATLAB, so I understand that my code might be unnecessarily long.
How do I put all of the strcat outputs in the same cell array (group) without having the code rewrite over the last output over and over again?
%set month and date vectors
month=['JanFebMarAprxxx']; %had to attach them bc k reads one alphabet as one column / xxx is bc MATLAB gives error message without it
max_date=[31 28 31 30];
%set constants
%month index (this reads one alphabet as one column)
k=1;
%date starts from 1
q=1;
%max_date index
m=1;
%saving separate files as ex)"Jan22"
for i=18:24:3000
%i+24 cannot exceed 2378, so i needs to be less than 2355
%I didn't attach this file here, so please disregard this ---------------------------
%if i<2355
%DataAdj=HoboData(i:(i+24),1)-hoboAB1;
%else
%end
%------------------------------------------------------------------------------------
%This code should give outputs such as 'Jan01', 'Feb28', and so on
if q<10 %if the date is less than 10
FileName=strcat(month(k),month(k+1),month(k+2),'0',num2str(q));
save FileName %DataAdj
clear FileName %DataAdj
q=q+1; %increase last date by 1
elseif q<=max_date(m) %if the date is less or equal to the max date (ex. 31, 28, 31, 30)
FileName=strcat(month(k),month(k+1),month(k+2),num2str(q));
save FileName %DataAdj
clear FileName %DataAdj
q=q+1; %increase last date by 1
else
q=1; %start date from 1 again
m=m+1; %max_date index increase by 1
%because there is no data after april, we have to limit k to k<4
k=k+3;
end
end
2 个评论
Walter Roberson
2020-8-1
months = {'Jan', 'Feb', 'Mar, 'Apr'};
FileName = sprintf('%s%02d', months{k}, q);
save(Filename);
回答(1 个)
Harsha Priya Daggubati
2020-7-31
Hi,
You can simply concatenate the new String to Filename as follows each time while assigning. This gives a FileName concatenated with all the strings you create.
%set month and date vectors
month=['JanFebMarAprxxx']; %had to attach them bc k reads one alphabet as one column / xxx is bc MATLAB gives error message without it
max_date=[31 28 31 30];
FileName = [];
%set constants
%month index (this reads one alphabet as one column)
k=1;
%date starts from 1
q=1;
%max_date index
m=1;
%saving separate files as ex)"Jan22"
for i=18:24:3000
%i+24 cannot exceed 2378, so i needs to be less than 2355
%I didn't attach this file here, so please disregard this ---------------------------
%if i<2355
%DataAdj=HoboData(i:(i+24),1)-hoboAB1;
%else
%end
%------------------------------------------------------------------------------------
%This code should give outputs such as 'Jan01', 'Feb28', and so on
if q<10 %if the date is less than 10
FileName=[FileName ,' ' ,strcat(month(k),month(k+1),month(k+2),'0',num2str(q))];
q=q+1; %increase last date by 1
elseif q<=max_date(m) %if the date is less or equal to the max date (ex. 31, 28, 31, 30)
FileName=[FileName ,' ', strcat(month(k),month(k+1),month(k+2),num2str(q))];
q=q+1; %increase last date by 1
else
q=1; %start date from 1 again
m=m+1; %max_date index increase by 1
%because there is no data after april, we have to limit k to k<4
k=k+3;
end
end
result = split(FileName)
Later you can use split on FileName, to get all the values.
2 个评论
Harsha Priya Daggubati
2020-8-3
I guess FileName doesn't show up since you are trying to clear the variable using 'clear'. Try removing 'clear' command and see.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!