How to load .dat (or even .txt ) files into matlab workspace?

조회 수: 13 (최근 30일)
Ray
Ray 2013년 2월 25일
댓글: Walter Roberson 2017년 5월 14일
Hello
I am new to matlab. I am trying to load .dat files from another folder into matlab. I could use .txt files if that is easier.
Here is code I am using:
directory_name=uigetdir(pwd,'Select data directory');
directory_name=([directory_name '\']);
directory_name=uigetdir(pwd,'Select data directory');
directory_name=([directory_name '\'])
files=dir([directory_name,'*.dat'])
This opens the diretory and I am able to choose the folder of choice. However it does not load the file only the filename. In the matlab workspace it shows that file structure to be 1X1.
If I add the following lines:
filename=files.name;
data=load(filename);
I get the following error, of course:
Error using load
Unable to read file 15a001sen2.dat: No such file or
directory.
Please advise how to load files. thanks!
  댓글 수: 2
Mrittika Mahbub
Mrittika Mahbub 2017년 5월 14일
Walter Roberson Can u give a example plz. I can't load .dat file
Walter Roberson
Walter Roberson 2017년 5월 14일
Mrittika Mahbub
I already gave an example.
However, there is no standard format for .dat files. You need to know the format of the particular .dat files you are interesting in to know the commands needed to load in the data.

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

채택된 답변

Walter Roberson
Walter Roberson 2013년 2월 25일
[filename directory_name] = uigetfile('*.dat', 'Select a file');
fullname = fullfile(directory_name, filename);
data = load(fullname);
  댓글 수: 1
Ray
Ray 2013년 2월 25일
편집: Ray 2013년 2월 25일
Thank you very very much. This code does exactly what I was trying to do, as is. Thank you once again!

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 25일
편집: Azzi Abdelmalek 2013년 2월 25일
fid = fopen([yourpath '/' 'file.txt']);
line1 = fgetl(fid);
res=line1;
while ischar(line1)
line1 = fgetl(fid);
res =char(res,line1)
end
fclose(fid);
res
  댓글 수: 1
Ray
Ray 2013년 2월 25일
편집: Ray 2013년 2월 25일
Thank you for your response. Instead of the first line of code I added the following: [ afile workdir ] = uigetfile( '*.dat','Select DAT-file' ); fid = fopen([workdir,afile])and then the rest of the code, however when I use this code, it loads an array of 2501 X 9 instead of 2500 X 1.

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

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by