convert .dat files into .txt or .xls files
Show older comments
Hi all, I am collecting spectra from a system that outputs data as .dat file. I'd like to convert these files in .txt or .xls to plot them. I tried this
clear all
close all
filename = 'finale100.dat';
A = importdata(filename);
Npoints = A.data(1);
for ii = 1:Npoints
intensity(ii,1) = str2num(A.textdata{8+ii});
end
plot(intensity)
but it's not working. I cannot attach it as it won't let me upload the .dat file.. Any suggestions please?
13 Comments
What do you mean by 'not working'? Do you get an error? Or an incorrect plot? And why do you need to save to .txt or .xls to plot? Your code shows nothing to do with saving to a different file format, which supports the fact that it is not a necessary part of plotting data.
So is your question about plotting or saving data to .txt or .xls?
On a side note, you should presize 'intensity' as e.g.
intensity = zeros( NPoints, 1 );
if you wish to assign to it in a loop like that as it is more efficient than growing it in a loop. M-Lint should highlight this with an orange underline that you should always take note of in your code.
Adam
on 10 Jul 2018
Well, it is working but it plots a totally different plot of what I am expecting (I can see the plot in real time while it's collecting so I have an idea on how the it looks like).
And why do you need to save to .txt or .xls to plot? can I plot from .dat then? I thought I cannot
So is your question about plotting or saving data to .txt or .xls? both as I get a wrong plot and I did not know I can plot from .dat files
Thanks for suggestions
Adam
on 10 Jul 2018
Well, I'm not familiar with .dat files, but it isn't really a question of plotting from a .dat file or from a .txt file or any other type per se.
There are two distinct steps - load your data into Matlab, plot it.
Assuming you load it correctly it can be in any file format you want and will still end up in Matlab in the same way, as variables that you can then plot, irrespective of where it was loaded from.
If you are getting the wrong plot then I imagine your code to read the data from .dat is incorrect in some way, i.e. this:
intensity(ii,1) = str2num(A.textdata{8+ii});
but I don't know what A contains. You can look at A in your workspace and ascertain that the data is correct or not at that stage, from your dat file. If it is then it is the above line that makes it incorrect since the plot instruction is as basic as can be.
Jan
on 10 Jul 2018
@Laura: The file extension ".dat" is not uniquely defined. You can append this file extension to all filenames without changing the contents of the file.
Maybe the code works correctly and only your expectations are the problem.
Guillaume
on 10 Jul 2018
Thank you both, I will try.
Guillaume
on 10 Jul 2018
Note that you can upload your .dat file simply by changing its extension to .txt
Laura Paletto
on 10 Jul 2018
We still don't know what the format of your .dat files are. Are they binary or ascii? Again though, there is no need to convert them to a different format, especially if you want to do it using Matlab. That would involve loading them correctly into Matlab as the first step (then saving them to the new format), but if you have managed to correctly load them into Matlab then you can just plot them.
Take a look at the 'A' variable that was loaded into your workspace. Does what is inside it make sense and correspond to what you expect to be in the .dat file?
Jan
on 10 Jul 2018
@Laura: Please post comments in the section for comments, not in the section for answers.
Guillaume
on 10 Jul 2018
That was another point. I have more then 1000 .dat files and converting them manually can take forever
What I suggested is to simply rename the file from finale100.dat to finale100.txt, nothing more. That way the forum will let you upload the file.
The code you've posted assumes that finale100.dat is a text file. A text file can have any extension you want (including misleading ones such as .exe or .jpg), it doesn't change anything about the content of the file.
Therefore to convert .dat files (that contain text) to .txt, there is nothing more to do than rename them.
Laura Paletto
on 10 Jul 2018
Just open it in any text editor (I use Notepad++, but basic Notepad would work) to see if it is binary or ascii. If it is human-readable then it is ascii. If it is full of symbols and junk it is binary. If A looks right though then again, it looks like your code on this line:
intensity(ii,1) = str2num(A.textdata{8+ii});
is not extracting the correct results from it. Why is there a hard-coded 8 in there?
Guillaume
on 10 Jul 2018
Just open it in any text editor
Or attach it here after renaming it. We'll tell you.
Answers (0)
Categories
Find more on Desktop in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!