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

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!

Related

MATLAB/Embedded Coder File Loading

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.

Open a text file, scan it and plot it in MATLAB

I am trying to open a text file in MATLAB and plot it in a graph. The following is my code:
%% Get the data
[filename, pathname] = uigetfile('*txt', 'Pick text file');
x=filename(:,1);
y=filename(:,2);
plot(x,y);
But each time I run it, I get following error:
Error using plot
Invalid first data argument.
Error in readtxtfile (line 5)
plot(x,y);
The text file that I imported has 2 rows. I am planning to plot the first row with the second say plot (row 1, row 2) in MATLAB.
You have the name of the file stored in filename combined with the path to the directory where the file is stored in pathname but you actually haven't read any of the contents. To do that, the easiest thing would be to use dlmread. I'm assuming your text file is properly formatted to have two rows of data as you stated. If this is the case, you need to change the way you're indexing into your data. You have it indexing entire columns instead of rows so you need to flip the indexing in your code. Also, you need a call to dlmread, then access the columns of the resulting matrix:
%% Get the data
[filename, pathname] = uigetfile('*txt', 'Pick text file');
data = dlmread(fullfile(pathname, filename));
x=data(1,:);
y=data(2,:);
plot(x,y);
Notice that I made the full path to your file to use fullfile because using uigetfile allows you to read in a file from anywhere on your computer so we make sure that we capture the full path to your file. Again to reiterate, pathname is the directory of where the file is contained and filename is the name of the file contained in the directory.

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)

How to store fig in .mat file?

Is it possible to store the matlab figure inside a mat file, where the variable are stored.
I got into a scenario where i generated some plot from the variable stored in the mat file. Currently im storing the figure as a separate file, this means, i have a 1 file for variables and another file for figure. But i would like to bundle them together in a single file.
How about selecting both files in the windows explorer and zip them? ;-)
Seriously, while I do not know of a way to do exactly what you want (what is it, exactly, anyway? Do you expect the figure to pop up once you've typed load variables.mat and pressed enter?) I see this way around it:
You could store the command(s) needed to generate the figure in an anonymous function or as a string and save it along with all other variables. Then, after loading the .mat file, you call that function or eval on the string and the figure will be regenerated.
x=sort(rand(1,100)); y=sort(randn(1,100)); %# sample data
makefig = #() plot(x,y,'g.'); %# anonymous figure-generating function
save myDataAndFigure
clear all
load myDataAndFigure
makefig()
...or, with a string (e.g. when including formatting and axis-labelling commands)
x=sort(rand(1,100)); y=sort(randn(1,100)); %# sample data
figcmd = 'plot(x,y,''g.''); xlabel(''sort(U(0,1)''); ylabel(''sort(N(1,0)'');'
save myDataAndFigure
clear all
load myDataAndFigure
eval(figcmd)
The latter should save memory when the involved data are large, since the anonymous function object contains all the data it needs, i.e. its own "copy" of x and y in the example.
There's an article here on fig file format and how it's actually a mat file in a disguise.
So you can take the fig and store its data in a structs and save them as a mat file, then load the mat file and make fig out of the structs you saved.
How about storing data and functions in instances of a class and unsing the functions later to plot the data?
Actually this is surprisingly easy to do.
Suppose you have just created the figure in question. Converting the figure handle into a struct yields the corresponding hierarchical elements (including the data, labels, everything) required to display the figure.
If desired, this struct may then be saved to a mat file just as though it was data. (It is, in fact.) To view the contents of the struct as a figure again simply reconvert it to a handle with struct2handle.
% The line below converts the current figure handle into a struct.
this_fig = handle2struct(gcf)
% The line below converts the struct "this_fig" back to a figure handle and displays it.
h = struct2handle(this_fig,0);

Reading in points from a file

I have a txt file in which each row has the x, y ,z coordinates of the point. seperated by space.I want to read points from this txt file and store it as a matrix in matlab of the form [Pm_1 Pm_2 ... Pm_nmod] where each Pm_n is a point .Could someone help me with this?
I have to actually enter it into a code which accepts the model as :
"model - matrix with model points, [Pm_1 Pm_2 ... Pm_nmod]"
I use importdata heavily for this. It reads all kinds of formats ; I normally use other methods like dlmread only if importdata doesn't work.
Usage is as simple as M = importdata('data.txt');
Just use
load -ascii data.txt
That creates a matrix called `data' in your workspace whose rows contain the coordinates.
You can find all the details of the conversion in the documentation for the load command.