MATLAB/Embedded Coder File Loading - matlab

I generated code for loading a mat file as follows
data=coder.load('data.mat');
a=data.a;
b=data.b;
Because one of the variables, for example "a", is very big, it is defined as a big static const array in the main function with all values initialized there.
Is there any way I can make MATLAB Coder load data from a file in C Code instead of defining it as a variable in main function?

The MATLAB fread function is supported for code generation. So you can fwrite the data to a file in MATLAB and then fread it in the generated code. This will do a runtime read and avoid the giant constant in the generated code.

This is the exact code that we should use based on Ryan's answer:
load('Data.mat')
fileID = fopen('Data.bin', 'w');
fwrite(fileID, Matrix1,'uint64');
fclose(fileID);
fileID=fopen('Data.bin');
Matrix2=fread(fileID,[256,256],'uint64');
fclose(fileID);
Matrix 2 is now the same as Matrix 1. The trick for writing and reading is to use the same precision based on the data type.

Related

Deriving 'y' matrix and sampling rate as the parameters of audiowrite function i.e audiowrite(filename,y,Fs)

I am a bit lost in understanding how audiowrite function derives 'y', and 'Fs', the last two parameters of the function. I looked at some of the examples provided by Matlab, and they seem to work fine. The first set of codes is an example by Matlab which returns 'y' and 'Fs' value.
load handel.mat
filename = 'handel.wav';
audiowrite(filename,y,Fs);
Now, I have written a similar set of codes to work with a .csv file. Knowing the input argument for load function can only be in .mat format, I have written some codes in the following format.
csvread('bgst.csv');
save bgst.mat
load bgst.mat;
filename = 'bgst.wav';
audiowrite(filename,y,Fs);
But, in this case I am getting the error message: Undefined function or variable 'y'(or, Fs if I have defined y in the line w/ csvread function). So, why audiowrite is not creating 'y', and 'Fs' by itself as in the previous case. BTW, my .csv file is 999998x4 double. And, any more information to help me out, please let me know.
When you do:
load handel.mat
You are loading a MAT-file containing variables into the workspace. This is what initializes y and Fs, which are used as input arguments to audiowrite. You can see this using the whos function to see the variables being created:
>> clear all % Removes all current variables
>> load handel.mat
>> whos
Name Size Bytes Class Attributes
Fs 1x1 8 double
y 73113x1 584904 double
The variable y contains the matrix of audio data with sampling rate Fs. You have to create these yourself for your data and pass them to audiowrite.

Read the data from specific line in MATLAB

I have a sequence of data files(".tab" files) with more than 11100 rows and 236 columns. Data begins from 297th line in one file and from 299th line in another file. How can I read the data from 297th row of each file in MATLAB R2014a?
I am not quite sure, bu it seems that a typical machine's memory can handle such a file size. In that case, you can use textscan or textread MATLAB built-in functions.
Nonetheless, if you really cannot import your data into MATLAB environment, set HeaderLines argument of textscan to the line of interest. A simple example can be found in MATLAB documentations, or:
SelectedData = textscan(ID,formatSpec,'HeaderLines',296); % Ignore 296 first lines of the data
First of all, I strongly recommend to review the MATLAB documentation. Assuming you have several files in hand (stored in fileNames:
for i = 1:numel(fileNames)
ID = fopen(fileNames{i});
formatSpec = '%s %[^\n]'; % Modify this based on your file structure
SelectedData{i} = textscan(ID,formatSpec,'HeaderLines',296);
fclose(ID);
end
SelectedData is a cell string containing all your data extracted from corresponding data (fileNames)

Exporting Matlab figure to .dxf using file exchange´s DXFLib

Problem
I am trying to export my matlab plot to a .dxf format. I am using this library DXFlib
I am using the following function of the library:
FID = dxf_polymesh(FID, VERTICES, FACES, varargin)
My plot is created of an alphashape:
h=plot(shp1,'FaceColor','red');
So I try to use the function by:
dxf_polymesh('test.dxf',h.Vertices,h.Faces)
Struct contents reference from a non-struct array object.
Error in dxf_polymesh (line 75) fclose(FID.fid);
In the test file of the library FID is described as follows:
FID is a structure containing handle to DXF file and parameters that
are used by remaining routines. DXFLib is a 'state' library.
Questions
This is probably a quite simple/stupid question:
What does FID in a content like this mean ( I know it stands for file identifier) but is this in this case the file I want to create, an existing file or something completly else ?
I guess I have some kind of wrong FID input from this error, what would be the right thing to enter in that function ?
Thanks for your help!

Simplest way to read space delimited text file matlab

Ok, so I'm struggling with the most mundane of things I have a space delimited text file with a header in the first row and a row per observation and I'd like to open that file in matlab. If I do this in R I have no problem at all, it'll create the most basic matrix and voila!
But MATLAB seems to be annoying with this...
Example of the text file:
"picFile" "subjCode" "gender"
"train_1" 504 "m"
etc.
Can I get something like a matrix at all? I would then like to have MATLAB pull out some data by doing data(1,2) for example.
What would be the simplest way to do this?
It seems like having to write a loop using f-type functions is just a waste of time...
If you have a sufficiently new version of Matlab (R2013b+, I believe), you can use readtable, which is very much like how R does it:
T = readtable('data.txt','Delimiter',' ')
There are many functions for manipulating tables and converting back and forth between them and other data types such as cell arrays.
There are some other options in the data import and export section of the Statistics toolbox that should work in older versions of Matlab:
tblread: output in terms of separate variables for strings and numbers
caseread: output in terms of a char array
tdfread: output in terms of a struct
Alternatively, textscan should be able to accomplish what you need and probably will be the fastest:
fid = fopen('data.txt');
header = textscan(fid,'%s',3); % Optionally save header names
C = textscan(fid,'%s%d%s','HeaderLines',1); % Read data skipping header
fclose(fid); % Don't forget to close file
C{:}
Found a way to solve my problem.
Because I don't have the latest version of MATLAB and cannot use readable which would be the preferred option I ended up doing using textread and specifying the format of each column.
Tedious but maybe the "simplest" way I could find:
[picFile subCode gender]=textread('data.txt', '%s %f %s', 'headerlines',1);
T=[picFile(:) subCode(:) gender(:)]
The textscan solution by #horchler seems pretty similar. Thanks!

MATLAB uint8 data type variable save

does anyone know how to save an uint8 workspace variable to a txt file?
I tried using MATLAB save command:
save zipped.txt zipped -ascii
However, the command window displayed warning error:
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'zipped' not written to file.
In order to write it, simply cast your values to double before writing it.
A=uint8([1 2 3])
toWrite=double(A)
save('test.txt','toWrite','-ASCII')
The reason uint8 can't be written is hidden in the format section of the save doc online, took myself a bit to find it.
The doc page is here: http://www.mathworks.com/help/matlab/ref/save.html
The 3rd line after the table in the format section (about halfway down the page) says:
Each variable must be a two-dimensional double or character array.
Alternatively, dlmwrite can write matrices of type uint8, as the other poster also mentioned, and I am sure the csv one will work too, but I haven't tested it myself.
Hopefully that will help you out, kinda annoying though! I think uint8 is used almost exclusively for images in MATLAB, but I am assuming writing the values as an image is not feasible in your situation.
have you considered other write-to-file options in Matlab?
How about dlmwrite?
Another option might be cvswrite.
For more information see this document.
Try the following:
%# a random matrix of type uint8
x = randi(255, [100,3], 'uint8');
%# build format string
frmt = repmat('%u,',1,size(x,2));
frmt = [frmt(1:end-1) '\n'];
%# write matrix to file in one go
f = fopen('out.txt','wt');
fprintf(f, frmt, x');
fclose(f);
The resulting file will be something like:
16,108,149
174,25,138
11,153,222
19,121,68
...
where each line corresponds to a matrix row.
Note that this is much faster than using dlmwrite which writes one row at a time