Can't Visualize Ascii File on Matlab - matlab

I have .txt file created by an STM32 connected to a accelerometer sensors.
This file are Ascii file (probabily 8bit)
I tried to visualize on Matlab using
ZZ = load('000_LOG.TXT', '-ascii')
Error using load
Unknown text on line number 1 of ASCII file 000_LOG.TXT
so i tried
zx = load ('000_LOG.TXT')
Error using load
Unknown text on line number 1 of ASCII file 000_LOG.TXT
so i tried after reading this post importing ASCII file to Matlab
fid = fopen ('000_LOG.TXT');
>> myData = fread ('fid')
Error using fread
Invalid file identifier. Use fopen to generate a
valid file identifier.
The command "importdata" worked, but i can't convert to dec
Xw = importdata ('000_LOG.TXT')
Xw =
'|}}|}}|~}|~}|~}|}~|}~}}~}}~}}}}}~}}~}}|}}}}|}|}}~}}}}}}}|€}|}|€}}€}|...'
In fact
Wx = bin2dec(Xw)
Error using bin2dec (line 35)
Binary string must be 52 bits or less.
This is the link to the file .txt on dropbox
https://www.dropbox.com/l/s/mDoDk5puIpZaEdLth4w0Lr
Thanks for your support

Related

Load matrix from .dat file Matlab (except remove first 8 rows)

To simplify my problem, I am trying to load a file named data.dat.
I however do not know how to import a only a specific number of rows. For example:
Blue Red Green
1 2 4
1 3 4
0.1 2.2 3
.
.
.
How do I go about only importing rows from 0.1 and below. I do not want the first 2 rows nor the headers.
I know this is a fairly simple problem but I keep running into the following error:
Error using textscan. Invalid file identifier. Use fopen to generate a valid file identifier.
fid = fopen('data.dat', 'r');
mat = textscan(fid, '%f', 'HeaderLines', 1);
fclose(fid);
I thought that this works by removing the first row but I am clearly mistaken.
You can use dlmread command like single line of the code below (I'm assuming that your file located at the current working directory, otherwise use the correct path):
mat = dlmread('data.dat','',4,0);
Note: You don't need to open/close the file.

Reading multiple .dat files into MatLab

I am having great difficulties reading a bunch of .dat files into MatLab. I have tried to Google the problem, but after one hour I still can't get my code to work. In total I have 141 .dat files. Each file consist of three lines of header information (which I do not want to include), and then a bunch of rows, each with three columns of numbers. I want to merge the rows from all the .dat files into one large matrix featuring all the rows, and three columns (since each row in all .dat files contains three numbers). This is the code I have attempted to use:
d = dir('C:\Users\Kristian\Documents\MATLAB\polygoner1\');
out = [];
N_files = numel(d);
for i = 3:N_files
fid = fopen(d(i).name,'r');
data = textscan(fid,'%f%f%f','HeaderLines',3);
out = [out; data];
end
However, when I try to run the code I get the error message
??? Error using ==> textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in ==> readpoly at 6
data = textscan(fid,'%f%f%f','HeaderLines',3);
If anyone knows how I can get this to work, then I would be extremely grateful!
The problem is when you use fopen, you are not giving the full path of the file
path = 'C:\Users\Kristian\Documents\MATLAB\polygoner1\'
d = dir(path);
....
%as #craigim advised it, otherwise you can use strcat
my_file = fullfile(path, {d.name})
for i = 3:N_files
fid = fopen(my_file{i},'r');
....
fclose(fid);
end

Determine number of bytes in a line of a binary file Matlab

I am working on importing some data interpretation of binary files from Fortran to MATLAB and have come across a bit of an issue.
In the Fortran file I am working with the following check is performed
CHARACTER*72 PICTFILE
CHARACTER*8192 INLINE
INTEGER NPX
INTEGER NLN
INTEGER BYTES
c This is read from another file but I'll just hard code it for now
NPX = 1024
NLN = 1024
bytes=2
open(unit=10, file=pictfile, access='direct', recl=2*npx, status='old')
read(10,rec=nln, err=20) inline(1:2*npx)
go to 21
20 bytes=1
21 continue
close(unit=10)
where nln is the number of lines in the file being read, and npx is the number of integers contained in each line. This check basically determines whether each of those integers is 1 byte or 2 bytes. I understand the Fortran code well enough to figure that out, but now I need to figure out how to perform this check in MATLAB. I have tried using the fgetl command on the file and then reading the length of the characters contained but the length never seems to be more than 4 or 5 characters, when even if each integer is 1 byte the length should be somewhere around 1000.
Does someone know a way that I can automatically perform this check in MATLAB?
So what we figured out was that the check is simply to see if the file is the correct size. In Matlab this can be done as
fullpath=which(file); %extracting the full file path
s=dir(fullpath); %extracting information about hte file
fid=fopen(file_name,'r'); %opening image file
if s.bytes/NLN==2*NPX %if the file is NLN*NPX*2 bytes
for n=1:NLN %for each line
dn(n,:) = (fread(fid, NPX, '*uint16','b'))'; %reading in lines into DN
end
elseif s.bytes/NLN==NPX %Else if the file is NLN*NPX bytes
for n=1:NLN %for each line
dn(n,:) = (fread(fid, NPX, '*uint8','b'))'; %reading in lines into DN
end
else %If the file is neither something went wrong
error('Invalid file. The file is not the correct size specified by the SUM file')
end
where file contains the filename, nln contains the number of lines, and npx contains the number of columns. Hope this helps anyone who may have a similar answer, but be warned because this will only work if your file only contains data that has the same number of bytes for each entry, and if you know the total number of entries there should be!
Generally speaking, binary files don't have line lengths; only text files have line lengths. MATLAB's getl will read until it finds the binary equivalent of newline characters. It then removes them and returns the result. The binary file, on the other hand, should read a block of length 2*npx and return the result. It looks like you want to use fread to get a block of data like this:
inline = fread(fileID,2*npx)
Your fortran code is requesting to read record nln. If the code you have shared reads all the records starting at the first one and working up, then can just put the above code in a loop for nln=1:maxValue. However, if you really do want to yank out record nln you need to fseek to that position first:
fseek(fileID, nln*2*npx, -1);
inline = fread(fileID,2*npx)
so you get something like the following:
Either reading them all in a loop:
fileID = fopen(pictfile);
nln = 0;
while ~feof(fileID)
nln = nln+1;
inline = fread(fileID,2*npx);
end
fclose(fileID);
or picking out only the number `nln record:
fileID = fopen(pictfile);
nln = 7;
fseek(fileID, nln*2*npx, -1);
inline = fread(fileID,2*npx);
fclose(fileID);

No data written to file in Matlab when using writeVideo (Ubuntu)

I am attempting to create a movie file from ascii data using Matlab. I am running Matlab on Ubuntu 13.10. When I run my Matlab script (see below), the frames display correctly and the .avi video file is created at the end, but there is no data in the file.
clc
clear all
writerObj=VideoWriter('testVideo');
writerObj.FrameRate = 10;
open(writerObj);
for i=1:15000
j=i*100;
str=[num2str(j),'_Temp1.dat'];
t=importdata(str);
showaxes;
colorbar;
imagesc(t);
set(gca,'ydir','normal')
frame=getframe;
writeVideo(writerObj,frame);
% movieFrames(:,i) = frame;
end
close(writerObj);
And in Matlab I get the error:
Warning: No video frames were written to this file.
The file may be invalid.
> In VideoWriter.VideoWriter>VideoWriter.close at 307
In VideoWriter.VideoWriter>VideoWriter.delete at 256
In videomaker at 2
Error using importdata (line 136)
Unable to open file.
Error in videomaker (line 11)
t=importdata(str);

Error when load file .dat in matlab

I have problem when I try to load file .dat in Matlab.
My file .dat about speech data:
% Read data file "orig.dat" with sampling rate of 8 kHz
% create an example sound
fs=8000;
t=0:1/fs:3;
x = 1*sin(2*pi*4*t)+0.25* sin(2*pi*560*t);
% play it back
%sound(x, 8000);
wavwrite(x,fs,16,'test56.wav');
y=wavread('test56.wav')
save y.dat y
load y.dat
There is a error:
??? Error using ==> load Number of columns on line 1 of ASCII file C:\Program Files\MATLAB\R2010b\bin\doan\y.dat
must be the same as previous lines.
Error in ==> twosubband at 8 load y.dat; % Load speech data
I don't understand.
Help me fix it.
load expects a file in its own data format. Try saving y as y.mat instead of y.dat.
That is, replace y.dat in both the line where you save and the line where you load by y.mat.
That should do the trick.