MATLAB Answers

Files not being read correctly

6 views (last 30 days)
Ibro Tutic
Ibro Tutic on 19 Nov 2015
Edited: Ibro Tutic on 19 Nov 2015
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
end %folders

  0 Comments

Sign in to comment.

Accepted Answer

Thorsten
Thorsten on 19 Nov 2015
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

  1 Comment

Ibro Tutic
Ibro Tutic on 19 Nov 2015
This was my original idea, but I need the composite file saved in the same file that the PIN are saved into. So I set up the folder name according to the pin I am using, but the composite file has a different naming system, so I need to somehow save it into the same file as the various .csv data that is located within the same file. I tried to put both of the saves inside of the else statement so that the file path is already defined, but that did not work as I wanted it to.
It looks like the composite file is always the last file in the fileinfo structure, is there a way to create an if else statement something like
I got it to work using my above idea, thanks! Using this code:
if fileidx == length(fileinfo)
csv read etc.etc.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by