How to create mat file containing video in it - matlab

I'm new to matlab programming.I have an image processing code which helps to load a mat file in it. the code accepts .mat file as input with video file in it.
filename=('C:\Users\HP\Desktop\Folder\Image\NVR_ch2_main_cut_35-41.asf');
s=load(filename);
s=struct2cell(s);
M=double(s{1});
if (length(size(M))==4)
M=squeeze(M(:,:,1,:));
end`
Error using load
Unknown text on line number 1 of ASCII file C:\Users\HP\Desktop\Folder\Image\NVR_ch2_main_cut_35-41.asf
"Seh".

Just use v = VideoReader(filename) instead of the load function.
For further information: http://ch.mathworks.com/help/matlab/ref/videoreader.html

Well obviously Matlab won't read your file because it contains things load won't accept.
Does your file comply to this: (from the Matlab reference , next time you should read this)
ASCII files must contain a rectangular table of numbers, with an equal
number of elements in each row. The file delimiter (the character
between elements in each row) can be a blank, comma, semicolon, or tab
character. The file can contain MATLAB comments (lines that begin with
a percent sign, %).
http://de.mathworks.com/help/matlab/ref/load.html#responsive_offcanvas
Read your first sentence. You say you want to load a .mat file. But filename ends with .asf which is some video format if I remember correctly.
You can't feed a video file into load.

Related

Matlab dataimport

My matlab code for dataimport is giving me different results for what appear to be similar text files as input. Input1 gives me a normal cell with all lines from the text file as entries in the cell which i can reference using {i}.
Input2 gives me a scalar data structure where all numeric entries in my text file are converted to the input.data structure. I want all files to be converted to regular cell entries and I do not understand why for some files they are converted to scalar data structures.
Code: input = importdata(strcat(direct,'\',filename));
Input1 example: Correctly working dataimport, with text file on the right
File link: https://drive.google.com/open?id=1aHK4xivqEqJEmaA8Dv8Y0uW5giG-Bbip
Input2 example: Incorrectly working data import, with text file on the right FIle link: https://drive.google.com/open?id=1nzUj_wR1bNXFcGaSLGva6uVsxrk-R5vA
UTSL!
I'm guessing you are using GNU Octave although you are writing "Matlab" as topic of your question.
In importdata.m around line 178, the code tries to automatically detect the delimiter for your data:
delim = regexpi (row, '[-+\d.e*ij ]+([^-+\de.ij])[-+\de*.ij ]','tokens', 'once');
If you run this against W40A0060; you get A as delimiter because there is basically a number before and after it.
If you run this against W39E0016; you get {} as delimiter(empty) because the E could be part of a number in scientific notation and is therefore excluded.
Solution:
you really should add the correct delimiter to the importdata call and not trust that it's magically detected.
And if you just want the lines in a cell, use
strsplit (fileread ("W39E0016_Input2.txt"), "\n")
Analysis
This looks indeed strange!
EDIT: The cause for this strange looking behaviour has been deciphered by #Andy (See his solution).
When you use all outputs of importdata() function you can see what happens when reading the data:
[dat1,del1,headerrows1]=importdata('Input1.txt')
[dat2,del2,headerrows2]=importdata('Input2.txt')
For your first file it recognizes 69 header riws and no delimiter:
del1 = []
headerrows1 = 69
while in your second file only two header rows and a comma , delimiter is recognized
del2 = ','
headerrows2 = 2
I can not find an obvious reason in your files causing this different interpretation of data.
Suggestion
Your data format is rather complex. It is not a simple table like produced from excel. It has multiple lines with a different number of fields per line and varying data types. importdata() is not designed for this type of data. I suggest to write a specific import function for this kind of file. Have a look at textread() for a first guess. You can use it to read the lines of the files as text and later interpret it with sscanf() or use strsplit() to split the line contents into fields.

How to extract numbers in a filename from matlab?

I have to read out numbers and possibly some letters from large set of file names in a directory. The file names have a format as "aXXXX_bXX_XX_S.ext" where 'X' could be any number and 's' could be any letter or a string. How do I extract those numbers and string as separate cell array?
Thanks!
First you can read all the files inside your directory. Assuming the location of your folder is stored in the string path, use:
a=dir(mypath);
Now you have a structure a. File names are stored in a.name. Now you can work with it. Here is a very rough code. You loop over all the files, check if the first letter is a (there could be some hidden files, you don't need them). Then you extract the data you need from the eligible files.
n=0;
for i=1:numel(a)
if a(i).name(1)=='a'
n=n+1;
numbers{n}=strcat(a(i).name(2:5),a(i).name(8:9),a(i).name(11:12));
letters{n}=a(i).name(13:find(a(i).name=='.')-1);
end
end

Read a string from text file returning strange characters in MATLAB

I'm reading a string like "1.0.2" from text file with these codes :
reader = fopen('Address\My_Text.txt');
Out= textscan(reader,'%str');
Out1=Out{1} ;
Out2=Out1{1};
fclose(reader);
This code (Out2) returns a string like this: 1.0.2 . This is a text file that copied by MATLAB from other place in HDD and read one time with above code for comparing with some existed text file and after that replace with this file using movefile (The main file is working correctly). When I create a text file manually and insert "1.0.2" in it, These codes read this value correctly. What is the problem? What is the solution for MATLAB?
Thanks.
You can use fopen('My_Text.txt', 'r', 'n', 'UTF-8') to open this file in UTF-8 encoding. For the added 3 parameters, check documentation of fopen for details.
Inserting fseek(reader, 3, 'bof') before textscan may also fix this problem, in a different manner.  is the BOM for UTF-8.

Send "\r\n" charactetusing GPIB

First, sorry if my english is not perfect.
I am using Matlab to generate a waveform and to create a binary file (based on on waveform points) which I want to send to an Agilent 33522A Function Generator to generate an arbitrary waveform. My problem is that my binary file generated in my computer contains "\r\n" characters, and when try to download data in 33522a it ends up with an error. I tried to change the EOSCharacter, but it is replaced wherever \n is found in my binary file.
If I generate the binary file so that all data is contaned in one line, I could send the data, but some information like, sample rate, number of samples and maximum /minimum voltage are not read by the Function Generator.
My binary file is something like that:
Line 1
Line 2
Line 3
Data:
integer value 1
intege value 2
...
I played with EOI mode, EOS mode and EOS Characther but I didn't succeeded to write in memory of Agilent 33522A a file with data written on more than one line;
If I put my data in a USB Stick, the binary file is correctly read by the aparat, even it contains \r\n character at the end of each line.
Any ideas on how to send thru GPIB a binary file which contains multiple CR and LF \r\n characters?
You have to set the EOSMode to 'write' by:
set(yourdevice_Obj,'EOSMode','write')
and leave the EOSCharacter as LF (\n).
an.

updating line in large text file using scala

i've a large text file around 43GB in .ttl contains triples in the form :
<http://www.wikidata.org/entity/Q1001> <http://www.w3.org/2002/07/owl#sameAs> <http://la.dbpedia.org/resource/Mahatma_Gandhi> .
<http://www.wikidata.org/entity/Q1001> <http://www.w3.org/2002/07/owl#sameAs> <http://lad.dbpedia.org/resource/Mohandas_Gandhi> .
and i want to find the fastest way to update a specific line inside the file without rewriting all next text. either by updating it or deleting it and appending it to the end of the file
to access the specific line i use this code :
val lines = io.Source.fromFile("text.txt").getLines
val seventhLine = lines drop(10000000) next
If you want to use text files, consider a fixed length/record size for each line/record.
This way you can use a RandomAccessFile to seek to the exact position of each line by number: You just seek to line * LineSize, and then update it.
It will not really help, if you have to insert a new line. Other limitations are: The file size will grow (because of the fixed record length), and there will always be one record which is too big.
As for the initial conversion:
Get the maximum line length of the current file, then add 10% for example.
Now you have to convert the file once: Read a line from the text file, and convert it into a fixed-size record.
You could use a special character like | to separate the fields. If possible, use somthing like ;, so you get a .csv file
I suggest padding the remaining space it with spaces, so it still looks like a text file which you can parse with shell utilities.
You could use a \n to terminate the record.
For example
http://x.com|http://x.com|http://x.com|...\n
or
http://x.com;http://x.com;http://x.com;...\n
where each . at the end represents a space character. So it's still somehow compatible with a "normal" text file.
On the other hand, looking at your data, consider using a key-value data store like Redis: You could use the line number or the 1st URL as the key.