How do i reset a fileID value?

Relatively new to MATLAB so it's probably a stupid question. But when writing my code I forgot to use the fclose function in my for loop so the value kept going up. My fid value i believe should be 34 but shows as 54 (my dataset has 34 colums). How do I sort of reset my fid value? also if anyone spots anything else wrong with the code that would be great. This is only a small portion of the code as I am just trying to get this part to work first.
close all
clear all
clc
fpath=uigetdir;
files=dir([fpath '\*.csv']);
files=char(files.name);
for n=1:size(files,1)
fid=fopen([fpath '\' files(n,:)]);
data{n}=textscan(fid, '%s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f','Delimiter',',','CollectOutput',1, 'Headerlines',1);
fclose(fid);
data{1,n}{1,1}=datenum(data{1,n}{1,1},'dd-mmm-yyyy HH:MM:SS');
end

1 comentario

Stephen23
Stephen23 el 25 de Nov. de 2022
Editada: Stephen23 el 25 de Nov. de 2022
Some tips on looping over filenames:
fpath = uigetdir;
files = dir(fullfile(fpath,'*.csv')); % use FULLFILE rather than text concatenation.
for n = 1:numel(files) % no need for a char array...
fnm = fullfile(fpath,files(n).name); % ... you can access the structure directly.
...
files(n).data = ... optional, if you want to store any data
end
As Image Analyst wrote in your other answer, you should not convert the filenames into a character array.

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 24 de Nov. de 2022
Using:
should close all of them. You can then start with fid = 3.
Also, I lost count of the number of '%f' there are. You can avoid writing all that with:
['%s' repmat('%f', 1, 33)]
ans = '%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f'
And I don’t understand using datenum here:
data{1,n}{1,1}=datenum(data{1,n}{1,1},'dd-mmm-yyyy HH:MM:SS');
The datetime function allows for sorting, comparisons, logical operations, and a number of others.
.

15 comentarios

Whale_Shark
Whale_Shark el 24 de Nov. de 2022
Thank you for your reply, you're an absolute star. With the datenum section, the first column of my data has the Date/Time (GMT) written as say
01-Jan-2015 02:30:00
so im trying to use a function to allow matlab to be able to read the data and was suggested the code above. How would I use the datetime function in this case?
As always, my pleasure!
I very much appreciate yoiur compliment!
I would do something like this —
str = '01-Jan-2015 02:30:00';
datetime(str, 'InputFormat','dd-MMM-yyyy HH:mm:ss') % NOTE — Format Differences
ans = datetime
01-Jan-2015 02:30:00
Or since there appears to be no ambiguity in the dates or times, simply —
datetime(str)
ans = datetime
01-Jan-2015 02:30:00
Also, if you use readtable, it should do the conversion for you automatically, as well as retain the column header data (variable names) that you can then reference.
It would help to know what version/release you’re using, since there have been changes in the last few releases.
.
Whale_Shark
Whale_Shark el 24 de Nov. de 2022
Hi! Thanks again for the speedy reply! The version of matlab I'm using is MATLAB R2022b. Basically the data that I am using is annual data from 1st Jan to 31st Dec and is im my data as shown:
Date/Time (GMT)
01-Jan-2015 00:00:00
01-Jan-2015 00:30:00
01-Jan-2015 01:00:00
01-Jan-2015 01:30:00
01-Jan-2015 02:00:00
01-Jan-2015 02:30:00
...and so on...
It is the first column of 34. I am working with ocean wave parameter data and need to create a FOR LOOP as I am comparing 5 years of data from two sites (each year is its down .csv data file). What I am aiming to achieve in this part of the code is that it can read all of the Date/Time (GMT) column data in the format as I will be needing to plot parameters such as wave height against time and such. I just can't seem to get it to be able to read the time data.
Thank you for all your help once again, its saving me from stress :D.
Star Strider
Star Strider el 24 de Nov. de 2022
I’m not certain what the problem could be. The ‘Date/Time (GMT)’ data should be automatically converted to a datetime array, since there appears to be no ambiguity, although it might require adding 'TimeZone','UTC'. It would help if you could attach one of your data files.
Whale_Shark
Whale_Shark el 25 de Nov. de 2022
Editada: Whale_Shark el 25 de Nov. de 2022
Here is one of the data files that i am working with. Each year is a different file, so I have five files from 2015 to 2019 for this site. Im having trouble getting it to read the data so I can't generate my variables for data such as wave height in MATLAB.
The readtable funciton imports the first column automatically as a datetime array, although it doesn’t have an associated 'TimeZone' property, so I set it to 'UTC'.
Assuming that ‘Hmax’ is the maximum wave height, I have no problems retrieving it and plotting it. (Plotting all of them reveals wave heights of 10000 meters, so I assumes that codes for missing data, since a wave height of that amplitude would be notable!)
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1207193/GdS_allwaveparameters2015.csv', 'VariableNamingRule','preserve')
T1 = 17520×34 table
Date/Time (GMT) Latitude Longitude Percentage of waves Hmax (m) Tmax (s) H1/10 (m) T1/10 (s) H1/3 (m) T1/3 (s) Hav Tav Eps Number of waves Tp (s) Direction (deg) Spread (deg) Tm02 (Tm)(s) Hm0 (Hs)(m) TI (s) T1 (s) Tc (s) Tdw2 (s) Tdw1 (s) Tpc (s) nu eps QP Ss Tref (deg C) SST (deg C) battery Te (s) P (kW/m2) ____________________ ________ _________ ___________________ ________ ________ _________ _________ ________ ________ _____ ___ _____ _______________ ______ _______________ ____________ ____________ ___________ ______ ______ ______ ________ ________ _______ _____ _____ ____ _____ ____________ ___________ _______ ______ _________ 01-Jan-2015 00:00:00 51.25 1.4832 100 1.46 4.7 1.07 4.5 0.87 4.5 0.55 3.8 0.515 469 5.6 184 10 4 0.99 4.9 4.3 3.1 4.5 5.7 5.7 0.356 0.642 2.49 0.04 9999 9999 9999 4.7 2.26 01-Jan-2015 00:30:00 51.25 1.4832 100 1.89 4.9 1.08 4.8 0.86 4.8 0.561 4.2 0.59 437 4.3 191 21 3.8 0.97 4.6 4 3 4.2 4.8 5.4 0.35 0.618 2.08 0.044 25.1 8.3 1 4.4 2.04 01-Jan-2015 01:00:00 51.25 1.4833 100 1.48 5.3 1.15 5.1 0.94 5.1 0.605 4.5 0.584 401 5.9 183 12 4.1 1.04 4.9 4.3 3.1 4.5 5.5 5.7 0.344 0.634 2.35 0.04 25.1 8.2 1 4.7 2.5 01-Jan-2015 01:30:00 51.25 1.4826 100 1.68 5.6 1.19 5.4 0.95 5.3 0.6 4.6 0.605 391 5.9 186 12 4.3 1.03 5.1 4.5 3.3 4.7 6 5.9 0.336 0.62 2.52 0.036 25.1 8.2 1 4.9 2.55 01-Jan-2015 02:00:00 51.25 1.4825 100 2.36 6 1.38 5.5 1.08 5.7 0.673 4.9 0.543 367 6.3 186 16 4.7 1.12 5.5 4.9 3.6 5.1 7.2 6.2 0.329 0.638 3.02 0.032 25.1 8.1 1 5.3 3.27 01-Jan-2015 02:30:00 51.25 1.4825 100 2.74 5.5 1.62 5.7 1.27 5.7 0.796 5 0.552 362 6.3 177 18 4.6 1.13 5.5 4.9 3.6 5.1 6.3 6.3 0.332 0.628 2.45 0.034 25.1 8.1 1 5.3 3.33 01-Jan-2015 03:00:00 51.25 1.4825 100 2.49 6.2 1.63 6.2 1.29 6.3 0.822 5.3 0.565 339 6.7 176 16 5 1.4 5.8 5.2 4 5.4 7.6 6.5 0.305 0.616 3.14 0.036 25.1 8 1 5.6 5.39 01-Jan-2015 03:30:00 51.25 1.4823 100 2.45 6.1 1.63 6.2 1.25 6.3 0.797 5.4 0.545 332 6.7 172 15 5.3 1.47 6.1 5.6 4.1 5.7 8.8 6.7 0.306 0.631 3.73 0.033 25.1 8 1 5.8 6.15 01-Jan-2015 04:00:00 51.25 1.4823 100 1.85 6.4 1.35 6.1 1.08 6.1 0.683 5.3 0.598 343 6.7 174 15 4.9 1.15 5.9 5.2 3.7 5.4 7.3 6.7 0.354 0.664 2.69 0.031 25.1 7.9 1 5.6 3.64 01-Jan-2015 04:30:00 51.249 1.4823 100 2.18 5.7 1.18 6 0.92 5.7 0.58 4.6 0.605 391 6.7 176 15 4.2 1.02 5.5 4.6 3.1 4.9 6.9 6.6 0.414 0.682 2.53 0.036 9999 9999 9999 5.2 2.66 01-Jan-2015 05:00:00 51.25 1.4823 100 1.44 5.9 1.11 5.6 0.93 5.6 0.59 4.5 0.631 402 6.7 176 14 4.1 0.96 5.5 4.5 2.8 4.9 7.3 6.7 0.464 0.726 2.78 0.037 9999 9999 9999 5.3 2.41 01-Jan-2015 05:30:00 51.25 1.4823 100 1.51 5.7 1.09 5.8 0.89 5.8 0.55 4.4 0.656 412 6.3 180 18 4.2 0.92 5.5 4.6 2.8 4.9 7.5 6.5 0.452 0.742 3.15 0.033 9999 9999 9999 5.2 2.17 01-Jan-2015 06:00:00 51.25 1.4824 100 1.89 5.9 1.18 5.7 0.94 5.7 0.59 4.7 0.68 385 6.3 188 20 4.4 1.45 5.3 4.7 3.2 5 7.1 6 0.37 0.69 3.17 0.047 9999 9999 9999 5.3 5.49 01-Jan-2015 06:30:00 51.25 1.4828 100 2.56 5.4 1.67 5.5 1.34 5.4 0.88 4.8 0.617 377 5.3 186 16 4 1.49 5 4.2 2.9 4.5 5.8 5.8 0.386 0.67 2.49 0.061 9999 9999 9999 4.8 5.25 01-Jan-2015 07:00:00 51.25 1.4828 100 2.48 4.9 1.68 5 1.3 5 0.82 4 0.583 446 5.9 188 24 3.8 1.43 4.8 4.1 2.9 4.3 5.4 5.7 0.382 0.648 2.2 0.062 9999 9999 9999 4.5 4.52 01-Jan-2015 07:30:00 51.25 1.4828 100 2.22 4.5 1.65 5 1.3 4.9 0.84 4 0.578 447 5.6 190 18 3.7 1.55 4.8 4 2.8 4.3 5.2 5.8 0.397 0.662 2.09 0.071 9999 9999 9999 4.6 5.45
VN = T1.Properties.VariableNames;
DT = T1.('Date/Time (GMT)')
DT = 17520×1 datetime array
01-Jan-2015 00:00:00 01-Jan-2015 00:30:00 01-Jan-2015 01:00:00 01-Jan-2015 01:30:00 01-Jan-2015 02:00:00 01-Jan-2015 02:30:00 01-Jan-2015 03:00:00 01-Jan-2015 03:30:00 01-Jan-2015 04:00:00 01-Jan-2015 04:30:00 01-Jan-2015 05:00:00 01-Jan-2015 05:30:00 01-Jan-2015 06:00:00 01-Jan-2015 06:30:00 01-Jan-2015 07:00:00 01-Jan-2015 07:30:00 01-Jan-2015 08:00:00 01-Jan-2015 08:30:00 01-Jan-2015 09:00:00 01-Jan-2015 09:30:00 01-Jan-2015 10:00:00 01-Jan-2015 10:30:00 01-Jan-2015 11:00:00 01-Jan-2015 11:30:00 01-Jan-2015 12:00:00 01-Jan-2015 12:30:00 01-Jan-2015 13:00:00 01-Jan-2015 13:30:00 01-Jan-2015 14:00:00 01-Jan-2015 14:30:00
T1.('Date/Time (GMT)').TimeZone = 'UTC'; % Set 'TimeZone' Property
T1.('Date/Time (GMT)').TimeZone % Check
ans = 'UTC'
figure
plot(T1.('Date/Time (GMT)'), T1.('Hmax (m)')) % Variable Name Reference
grid
xlim([0 hours(24)]+T1{1,1})
xlabel(VN{1})
ylabel(VN{5})
title('First 24 Hours Hmax')
figure
plot(T1{:,1}, T1{:,5}) % Column Index Reference
grid
xlim([0 hours(24)]+T1{1,1})
xlabel(VN{1})
ylabel(VN{5})
title('First 24 Hours Hmax')
This would likely require the Mapping Toolbox to plot with respect to the geographic coordinates. Since I don’t have it (and so have very little experience with it other than here on Answers), I defer to you for that.
.
Whale_Shark
Whale_Shark el 25 de Nov. de 2022
You are a saint, thank you.
Star Strider
Star Strider el 25 de Nov. de 2022
As always, my pleasure!
Whale_Shark
Whale_Shark el 25 de Nov. de 2022
Just seen your other comment on the other question. Thanks again for all your help!
Whale_Shark
Whale_Shark el 25 de Nov. de 2022
Editada: Whale_Shark el 25 de Nov. de 2022
Good person, it seems i require your assistance once again. So I was typing in the code line by line just to see if I typed it in correctly but now I get the error code:
Dot indexing is not supported for variables of this type.
Here is what I've typed in:
close all
clear all
clc
%Load in the data
fpath=uigetdir;
files=dir([fpath '\*.csv']);
files=char(files.name);
%for loop to go through the variable files, creating variables for wave
%height (Hmax [H])), max period (Tmax [T]), and geographical direction in
%degrees (Directiondeg [direction]). Using textscan function to read the
%data files.
for n=1:size(files,1)
fid=fopen([fpath '\' files(n,:)]);
data{n}=textscan(fid, ['%s' repmat('%f', 1,34)],'Delimiter',',','CollectOutput',1, 'Headerlines',1);
fclose(fid);
%readmatrix to set datetime array for first column in the data.
T1=readmatrix([fpath '\' files(n,:)]);
VN=T1.Properties.VariableNames;
DT=T1.('Date\Time (GMT)');
T1.('Date\Time (GMT)').TimeZone='UTC';
T1.('Date\Time (GMT)').TimeZone
end
It produces T1 and the data is there, but the time column shows as NaN due to the dot indexing error.
Star Strider
Star Strider el 25 de Nov. de 2022
The first instance I see of any dot indexing is in the ‘VN’ assignment. My code uses readtable, not readmatrix. Use readtable instead and see if the error persists.
Check to see what ‘T1’ is after that assignment. It should be a table and the rest of the code should then work correctly.
(Otherwise, it is possible that the file itself was not actually read, so ‘T1’ is empty.)
Whale_Shark
Whale_Shark el 25 de Nov. de 2022
Editada: Whale_Shark el 25 de Nov. de 2022
So the readmatrix was me being an idiot and not realising that i had typed readmatrix instead of readtable. It now produces the variables with the headers VN and T1 with the data values (see imported picture below),
but produces a warning and anothe error (see imported picture below).
close all
clear all
clc
%Load in the data
fpath=uigetdir;
files=dir([fpath '\*.csv']);
files=char(files.name);
%for loop to go through the variable files, creating variables for wave
%height (Hmax [H])), max period (Tmax [T]), and geographical direction in
%degrees (Directiondeg [direction]). Using textscan function to read the
%data files.
for n=1:size(files,1)
fid=fopen([fpath '\' files(n,:)]);
data{n}=textscan(fid, ['%s' repmat('%f', 1,34)],'Delimiter',',','CollectOutput',1, 'Headerlines',1);
fclose(fid);
%readtable to set datetime array for first column in the data.
T1=readtable([fpath '\' files(n,:)]);
VN=T1.Properties.VariableNames;
DT=T1.('Date/Time (GMT)');
T1.('Date/Time (GMT)').TimeZone='UTC';
T1.('Date/Time (GMT)').TimeZone
end
Star Strider
Star Strider el 25 de Nov. de 2022
Editada: Star Strider el 25 de Nov. de 2022
It’s not an error, simply a Warning.
My original code solves that as well:
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1207193/GdS_allwaveparameters2015.csv', 'VariableNamingRule','preserve')
Follow the arrows to the solution.
.
Whale_Shark
Whale_Shark el 25 de Nov. de 2022
You, Star Strider, are a heaven sent angel (other good beings and planes of existance are available). Seriously, thank you for all your help!!!
Star Strider
Star Strider el 25 de Nov. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 25 de Nov. de 2022

0 votos

@Victoria Snell regarding your dot indexing problem, did you not see my Answer in your duplicate question:
I show you how to fix that by creating the filename properly.

Categorías

Preguntada:

el 24 de Nov. de 2022

Editada:

el 25 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by