Files not being read correctly
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi, I have a script that is meant to convert a lot of .csv files into .mat for easier data manipulation. In these folders, the very first file is a always a file with the word 'composite' in it and it is formatted differently, so I need to handle them differently in my script. Attempting to read everything without the if ~isempty (idx) gives me an error in b, because the file name is -F4-(where - are underscores) and does not contain 4 letters which b looks for. Now I need this specific code to name the folders in the way the script was requested to name them, but I can't figure out how to get the code to work with the composite files. I test the code without any composite files and remove that if ~isempty (idx) statement and everything works perfectly. I get a PIN structure with loadprofile, rpm, and then a structure (1xnumber of files) with all the other data. When I try to include a way to read and save these composite files, a single pin is saved and the program stops. I need it to save the composite data as the original file name, which I think it accomplishes but it doesn't work long enough to get this far, its almost like it skips the if statement completely.
Running it as is gives the error: Error using save Variable 'PINC' not found.
Error in CSVtoMatLabBare (line 74) save(newfilename,'PINC');
Which tells me it skips the if statement completely even though the first file is always the composite file, any ideas?
clear all close all clc
%%%%%%%%%%%%%%%%%%%%%%%%%%File Pathing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
projectdir = 'C:\Users\it58528\Documents\Dig Test';
newdir = 'C:\Users\it58528\Desktop\Test';
folderinfo = dir(projectdir);
folderinfo = folderinfo([folderinfo.isdir]); %select only the directories
folderinfo = folderinfo(~ismember({folderinfo.name}, {'.', '..'})); %remove directories . and ..
%%%%%%%%%%%%%%%%%%Digs for Files/Reads/Saves %%%%%%%%%%%%%%%%%%%%%
for folderidx = 1 : length(folderinfo)
thisfolder = fullfile(projectdir, folderinfo(folderidx).name);
subfolderinfo = dir(thisfolder);
subfolderinfo = subfolderinfo([subfolderinfo.isdir]); %select only the directories
subfolderinfo = subfolderinfo(~ismember({subfolderinfo.name}, {'.', '..'})); %remove directories . and ..
folderidxi = folderinfo(folderidx).name;
newfolder = fullfile(newdir, folderidxi);
mkdir(newfolder);
for subfolderidx = 1 : length(subfolderinfo)
subfolderi = subfolderinfo(subfolderidx).name;
thissubfolder = fullfile(thisfolder, subfolderi);
fileinfo = dir( fullfile(thissubfolder, '*.csv') );
for fileidx = 1 : length(fileinfo)
filenamei = fileinfo(fileidx).name;
thisfile = fullfile(thissubfolder, filenamei);
[filepath, basename] = fileparts(thisfile);
thisfile = lower(thisfile);
idx = strfind(thisfile,'composite');
if ~isempty(idx)
datac = csvread(thisfile,5,2);
PINC(1).loadprofile = datac(16,:);
PINC(1).hours = sum(sum(PINC(1).loadprofile,1));
else
data = csvread(thisfile,5,2);
loadpercent = csvread(thisfile, 5,1,['B6..B20']);
fileID = fopen(thisfile);
C=textscan(fileID,'%s %s %s %s');
fclose(fileID);
a=C{1,3}{1,1};
b=a(1:4);
c=isstrprop(b,'alpha');
if c(1) == 1;
filename=strcat(a(1:2),subfolderi);
else
filename=strcat(a(2:3),subfolderi);
end
PIN(1).loadpercent =(fliplr(loadpercent'));
PIN(1).RPM = (data(16,:));
PIN(1).PIN(fileidx).PIN = C{1,3}{1,1};
PIN(1).PIN(fileidx).serialnumber = C{1,4}{2,1};
PIN(1).PIN(fileidx).loadprofile = data(1:15,:);
PIN(1).PIN(fileidx).hours = sum(sum(PIN(1).PIN(fileidx).loadprofile,1));
newsubfolder = fullfile(newfolder, filename);
mkdir(newsubfolder);
newfilename = fullfile(newsubfolder, filename);
save(newfilename,'PINC');
newfilename = fullfile(newsubfolder, filename);
save(newfilename,'PIN');
end
end %files within subfolder
end %subfolders within folder
채택된 답변
Thorsten
2015년 11월 19일
The problem in your code is that you do not save anything for the composite file. Also the first
save(newfilename,'PINC');
has no effect because it is overwritten by the following
save(newfilename,'PIN');
because newfilename is the same in both cases.
Use strcmp instead of strfind and isempty, and structure your code according to
if strcmp(thisfile, 'composite')
datac = csvread(thisfile,5,2);
PINC(1).loadprofile = datac(16,:);
PINC(1).hours = sum(sum(PINC(1).loadprofile,1));
% TODO: determine newfilename for composite files
save(newfilename,'PINC');
else
% all the code to process other files
% just save PIN
save(newfilename,'PIN');
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!