Reading a file and loading in to matlab.

I have downloaded a typical dataset from IMS BearingDataset (4. BearingDataset). It contains 3 folders and there are files with names like '2003.10.22.06.24' and the last 3 numbers varies. It shows the file extension as the last number(like .24 file format for the above file mentioned). I can open these files with notepad. When I open this the data is like
-0.022 -0.039 -0.183 -0.054 -0.105 -0.134 -0.129 -0.142
-0.105 -0.017 -0.164 -0.183 -0.049 0.029 -0.115 -0.122
-0.183 -0.098 -0.195 -0.125 -0.005 -0.007 -0.171 -0.071
-0.178 -0.161 -0.159 -0.178 -0.100 -0.115 -0.112 -0.078
-0.208 -0.129 -0.261 -0.098 -0.151 -0.205 -0.063 -0.066
-0.232 -0.061 -0.281 -0.125 0.046 -0.088 -0.078 -0.078
-0.112 -0.132 -0.181 -0.186 -0.132 -0.051 -0.132 -0.076
-0.054 -0.107 -0.173 -0.134 -0.164 0.002 -0.146 -0.125
-0.159 -0.032 -0.161 -0.181 -0.110 -0.044 -0.173 -0.137
-0.225 -0.044 -0.090 -0.159 -0.100 -0.151 -0.139 -0.076
-0.093 -0.117 -0.039 -0.161 -0.132 -0.161 -0.090 -0.098
-0.002 -0.161 -0.042 -0.054 -0.095 -0.232 -0.137 -0.042
0.000 -0.117 -0.081 -0.088 -0.142 -0.183 -0.117 -0.171
-0.154 -0.142 -0.027 -0.093 -0.183 -0.251 -0.095 -0.083
The columned data belongs to one bearing data . Now I want to load this in to a matrix format. Let me know how do i load this kind of file format in to matlab. I have tried using dlmread
M = dlmread('D:\Data_IMS Bearing\1st_test\2003.10.22.12.06.24');
it shows error
Error using dlmread (line 61)
The file 'D:\Data_IMS Bearing\1st_test\2003.10.22.12.06' could not be opened because: No such file or directory
Error in loadbearingdata (line 24)
M = dlmread('D:\Data_IMS Bearing\1st_test\2003.10.22.12.06.24');
I tried renaming it as txt file but even then it shows error.
Do any one know how to read these kind of files ? Thanks, Raady

7 commentaires

Image Analyst
Image Analyst le 1 Sep 2016
You forgot to attach the file, so no one can even try anything yet. Please attach it.
per isakson
per isakson le 1 Sep 2016
Modifié(e) : per isakson le 1 Sep 2016
The file itself is most likely not the problem (R2016a). (I downloaded from NASA.)
>> num = dlmread( 'h:\m\cssm\2003.10.22.12.06.24' );
>> whos num
Name Size Bytes Class Attributes
num 20480x8 1310720 double
"No such file or directory" indicates that the file is not found. What does
exist( 'D:\Data_IMS Bearing\1st_test\2003.10.22.12.06.24' )
return?
Raady
Raady le 1 Sep 2016
Modifié(e) : Raady le 1 Sep 2016
Image Analyst: I have updated with an attachment. That is kind of one of the datafile that I have in the whole pool when I downloaded the dataset. These will be multiple number in each folder, so in zip its one of them.
Raady
Raady le 1 Sep 2016
Modifié(e) : per isakson le 1 Sep 2016
per isakson
exist( 'D:\Data_IMS Bearing\1st_test\2003.10.22.12.06.24' )
ans =
0
per isakson
per isakson le 1 Sep 2016
0 says that Matlab doesn't find the file.
Raady
Raady le 1 Sep 2016
Modifié(e) : Raady le 1 Sep 2016
I have uploaded the sample , for a known path for this sample I want to read the contents and load data in to variable which is [no_of_rows x 8_columns] size.
per isakson
per isakson le 1 Sep 2016
Modifié(e) : per isakson le 1 Sep 2016
"I want to read the contents" &nbsp Yes, but you have most likely made a typo or something, which causes the error. See @Images answer.

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 1 Sep 2016

1 vote

It read in just fine for me. But the file you uploaded was NOT '2003.10.22.12.06.24', it was '2003.10.22.12.06.24.24'.
There are two 24's at the end of the filename, NOT just one. Make sure you're specifying the correct filename.

5 commentaires

Here is my Code which I was using in the loop.
G = folderFiles('D:\Data_IMS Bearing\1st_test','*2003.*');
len = length(G);
for n = 1 %:len
filename = G(n,:);
M = dlmread(filename);
mydata = M(:,[1,3,5,7]);
c = randperm(lenght(mydata),1500);
myreqdata = mydata(c,:);
end
% sampledata = myreqdata;
%end
I get error in dlmread as mentioned above.
dlmread() works fine on it. You're messing up on the filename. Try this. I tried it and it works fine.
folder = 'D:\Data_IMS Bearing\1st_test'
filePattern = fullfile(folder, '*2003.*');
G = dir(filePattern);
numberOfFiles = length(G)
for n = 1 : numberOfFiles
baseFileName = G(n).name;
fullFileName = fullfile(folder, baseFileName);
M = dlmread(fullFileName);
mydata = M(:,[1,3,5,7]);
c = randperm(length(mydata),1500);
myreqdata = mydata(c,:);
end
Raady
Raady le 1 Sep 2016
Modifié(e) : Raady le 1 Sep 2016
Thank you for suggestions per isakson and Image analyst ! inside loop I modified as
for n = 1%:len
filename = ['D:\Data_IMS Bearing\1st_test\',G(n,:)];
M = dlmread(filename);
end
Image Analyst
Image Analyst le 1 Sep 2016
It's "safer" to use fullfile() instead of [].

Connectez-vous pour commenter.

Plus de réponses (1)

michio
michio le 1 Sep 2016
If you are working on Windows system, I am guessing you can rename all the files to *.txt using the following command
system('ren ????.??.??.??.??.?? ????.??.??.??.??.??.txt')
Then the following would do the job.
M = dlmread('1st_test\2003.10.22.12.06.24.txt');
If you are using R2014b or newer, I'd suggest you use datastore instead. It could make your code simpler. For example,
ds = datastore('1st_test\*.txt','Whitespace',' \b','Delimiter','\t');
ds.ReadSize = 'file';
ds.VariableNames = {'B1x','B1y','B2x','B2y','B3x','B3y','B4x','B4y'};
data = read(ds);
read(ds) will read in the first file.
ds.SelectedVariableNames = {'B1x'};
allB1xdata = readall(ds);
will read the fist column from all files in the folder "1st_test".

2 commentaires

Raady
Raady le 1 Sep 2016
Modifié(e) : Raady le 1 Sep 2016
Michio Inoue: I cannot rename it as txt file either. I have updated with a sample in my Question attachments.
It seems that the variable "filename" of your code only holds the name of the file.
dlmread(filename);
is, for example, executing
dlmread('2003.10.22.12.06.24');
I'm not sure what is your current directory is, but how's
dlmread(['D:\Data_IMS Bearing\1st_test', filename]);
instead.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Files and Folders dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by