How to load a file
1 次查看(过去 30 天)
显示 更早的评论
Hi,
To load a file, I always do this: load file.txt
But this time, my file looks like what I have pasted below.
‘load’ does not work.
How can I load a file so I can put the data into an array?
Thank you for your help.
Herve
/********************************/
The file
(6.278305337995641,-31.165930909567250)
(1.758723282034028,-1.229391225298848)
(1.133464180020400,-3.392317005428029E-001)
(3.528019029863541,9.833493189283731E-001)
(-1.589297342018588,-6.805368090065715E-001)
(-5.740909803957738E-002,5.972384666809710E-001)
(2.903057010297026E-001,-1.201455315950079E-001)
(8.626581493421666E-001,-6.836608234777217E-002)
(-1.805284473824767E-001,-5.614038990230759E-001)
(2.126569179381342,-2.150085377308474)
…
0 个评论
采纳的回答
Laura Proctor
2011-10-21
The following code snippet will load your data into the matrix data:
fid = fopen('TestFile.txt');
M = textscan(fid,'(%f,%f)');
data = [ M{:,1} M{:,2} ];
fclose(fid);
5 个评论
Image Analyst
2011-10-22
Make sure your file is in the same directory as your m-file, or else prepend the full folder name to your base file name. Here's an example:
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
Image Analyst
2011-10-22
Sorry, but comments don't allow for code formatting so it looks kind of messed up. - no indentations, etc.
更多回答(1 个)
Kelly Kearney
2011-10-21
Textscan is probably your best bet here, assuming you want an n x 2 array of the paired data points. Try:
fid = fopen('file.txt');
data = textscan(fid, '(%f,%f)', 'collectoutput', true);
fclose(fid);
data = data{1};
3 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!