How do i reset a fileID value?

조회 수: 13 (최근 30일)
Whale_Shark
Whale_Shark 2022년 11월 24일
편집: Stephen23 2022년 11월 25일
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
Stephen23
Stephen23 2022년 11월 25일
편집: Stephen23 2022년 11월 25일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2022년 11월 24일
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
Whale_Shark
Whale_Shark 2022년 11월 25일
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 2022년 11월 25일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 11월 25일
@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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by