Reading a file and loading in to matlab.

8 views (last 30 days)
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 Comments
Raady
Raady on 1 Sep 2016
Edited: Raady on 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 on 1 Sep 2016
Edited: per isakson on 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.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 1 Sep 2016
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 Comments
Raady
Raady on 1 Sep 2016
Edited: Raady on 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 on 1 Sep 2016
It's "safer" to use fullfile() instead of [].

Sign in to comment.

More Answers (1)

michio
michio on 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 Comments
Raady
Raady on 1 Sep 2016
Edited: Raady on 1 Sep 2016
Michio Inoue: I cannot rename it as txt file either. I have updated with a sample in my Question attachments.
michio
michio on 1 Sep 2016
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!