필터 지우기
필터 지우기

how to create a .dat files

조회 수: 30 (최근 30일)
Carey-anne
Carey-anne 2014년 5월 26일
댓글: Walter Roberson 2018년 2월 3일
I have an .xls file, with 60 arrays of purely numeric values (no variable names or dates) that i would like to convert to a .dat file. I've tried importing the xls into Matlab and saving it as a .dat file. This creates a .dat file but i get the following error when i try to load it:
>> load xdata.dat
??? Error using ==> load
*Number of columns on line 1 of ASCII file* xdata.dat
*must be the same as previous lines.*

답변 (2개)

Image Analyst
Image Analyst 2014년 5월 26일
Use xlsread() to get the raw data (the third output argument.) Then use fprintf() to write out a .dat file in text form, or fwrite() to write out as binary. Examples are in the help for those functions.
  댓글 수: 3
Image Analyst
Image Analyst 2014년 5월 27일
Num is probably a cell array, especially if there are 60 separate tables of various sizes, so you can't write it out like that. You have to write out each cell one at a time (untested code follows).
for col = 1 : cols
for row = 1 : rows
if ~isnan(num{row, col})
fprintf(fid, '%f, ', num{row, col});
end
end
fprintf(fid, '\n');
end
Please read the FAQ for a better understanding of cell arrays.
By the way you have to use a backslash with \n, not a forward slash.
Carey-anne
Carey-anne 2014년 5월 27일
Thanks much for the clues, I discovered an easier code:
close all;
clear all;
%load excel file 65 arrays with 64 datapoints for each array
[num, txt, raw] = xlsread ('xdata.xls');
%write raw data to .dat file
dlmwrite('xdata.dat',raw, ' ')
% load .dat file
load xdata.dat

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


Jerin Joseph Koshy
Jerin Joseph Koshy 2018년 2월 3일
편집: Walter Roberson 2018년 2월 3일
space = ' '; % placeholder between the columns
%%open the file
tfile = fopen([filename '.dat'], 'w'); % overwrites existing file!
%%write the table top
for index = 1 : 2 : length(varargin)
fprintf(tfile, char(varargin(index)));
fprintf(tfile, space);
end
%%write the data
for index1 = 1 : length(varargin{2})
fprintf(tfile, '\r\n'); % newline
for index2 = 2 : 2 : length(varargin)
% values from data-vectors
fprintf(tfile, '%12.9f', varargin{index2}(index1));
fprintf(tfile, space);
end
end
%%close the file
fclose(tfile);
tfile value is showing -1 and getting error as
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in CreateTexDat (line 28)
fprintf(tfile, char(varargin(index)));
any solution??????
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid = fopen ('xdata.dat', 'w');
>> fclose(fid)
Error using fclose
Invalid file identifier. Use fopen to generate a valid file identifier.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  댓글 수: 2
Image Analyst
Image Analyst 2018년 2월 3일
Jerin, what is this about? Is it your "Answer" to Carey-anne's question? It looks like there is an error, so what is she supposed to do with this answer?
Are you sure you posted this on the right/intended page? Do you have a question?
Walter Roberson
Walter Roberson 2018년 2월 3일
You do not have write access to the directory you are cd'd to.

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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by