Borrar filtros
Borrar filtros

Saving data in separate folders under different file names

3 visualizaciones (últimos 30 días)
Jiwoo Seo
Jiwoo Seo el 25 de Sept. de 2020
Editada: Cris LaPierre el 26 de Sept. de 2020
Hi all,
I'm trying to put temperature data over a few months into separate folders per day under different file names containing the temperature data of that respective day. For example, it would look like this:
In folder 'Jan01' is a MATLAB file 'Jan01.mat', which contains temperature data collected on January 1st. This should happen for all dates up to April 30th.
Here is my full code for reference to constant values:
%Purpose of this program:
%This is to save all hobo data into separate .mat files under their
%respective dates.
clc
clear
%all temperature values are in Kelvin
%call hobo offset values
run('HoboConstants.m')
%open all hobo data from Jan23 to Apr30
HoboData_C=readmatrix('AB1_Jan22_to_May3.csv','Range','B234:C2393');
HoboData=HoboData_C(:,2)+273.15;
%set month and date vectors
month={'Feb','Mar','Apr'}; %had to attach them bc k reads one alphabet as one column
max_date=[28 31 30];
FileName1={};
DataAdj=[];
FullHoboAdj=[];
DailyTemp1={};
%set constants
%for month (this reads one alphabet as one column)
k=1;
%for date
q=1;
%index for max_date
m=1;
%saving separate files as ex)"Jan22"
for i=1:24:2160
DataAdj=HoboData(i:(i+23),1)-hoboAB1;
FullHoboAdj=[FullHoboAdj;DataAdj];
%'%s' means string and '%d' means number. If I want to increase the
%number, (ex. Jan002 instead of Jan02), I would put '%03d' instead
%of '%02d'.
if q<=max_date(m)
z=length(FileName1);
%this will name FileName1 as 'Jan01' up to 'Apr29'
FileName1{z+1}=sprintf('%s%02d',month{k},q);
%flipping the horizontal FileName1 to be vertical
FileName=reshape(FileName1',{},1);
%save the file in a separate folder, under the name of the correct
%date, with the respective hobo temp data
save('FileName','DataAdj'); %this saves only DataAdj under a single file named 'FileName.mat'
q=q+1;
else
q=1;
if m<=length(max_date)
m=m+1;
else
end
%because there is no data after april, we have to limit k to k<3
if k<=length(month)
k=k+1;
else
end
end
end
Here is the chunk of the code with the problems:
if q<=max_date(m)
z=length(FileName1);
%this will name FileName1 as 'Jan01' up to 'Apr29'
FileName1{z+1}=sprintf('%s%02d',month{k},q);
%flipping the horizontal FileName1 to be vertical
FileName=reshape(FileName1',{},1);
%save the file in a separate folder, under the name of the correct
%date, with the respective hobo temp data
save('FileName','DataAdj'); %this saves only DataAdj under a single file named 'FileName.mat'
q=q+1;
There is an error in my code that I do not understand. It only goes up to April 29th, when it's supposed to go up to April 30th.
The save function never seems to work. Please help me!
Thank you

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 25 de Sept. de 2020
Editada: Cris LaPierre el 26 de Sept. de 2020
I notice the lengh of the vector used for i is 90, but the length of Filename is 88. I suspect what is happening is that, when your code enters the else part of the if statement, you lose a data point, since a filename is not created in that loop. Since there are 89 days Feb - Apr (28+31+30), your vector for i reaches the end before you get to that last loop.
However, I don't think the correct strategy is to increase i. I think you want to fix your logic so that you don't skip a filename when the code in else is run. That means you actually need to decrease 2160.
Perhaps try something like this (I've simplified as much as possible)
%set month and date vectors
month={'Feb','Mar','Apr'}; %had to attach them bc k reads one alphabet as one column
max_date=[28 31 30];
FileName1={};
%set constants
k=1;
q=1;
m=1;
%saving separate files as ex)"Jan22"
for i=1:24:2136 %### decreased 2160 by 24 so that loop runs 89 times ###
if q>max_date(m)
q=1;
if m<=length(max_date)
m=m+1;
end
%because there is no data after april, we have to limit k to k<3
if k<=length(month)
k=k+1;
end
end
% ### Moved your code for creating FileName here ####
z=length(FileName1);
%this will name FileName1 as 'Jan01' up to 'Apr29'
FileName1{z+1}=sprintf('%s%02d',month{k},q);
%flipping the horizontal FileName1 to be vertical
FileName=reshape(FileName1',{},1);
q=q+1;
end
  1 comentario
Jiwoo Seo
Jiwoo Seo el 25 de Sept. de 2020
Thank you so much! The dates go up to Apr30 and I am so happy to finally stop worrying about this.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by