File Exchange

image thumbnail

SVG import-export

version 1.0.0.0 (157 KB) by martin de la gorce
Matlab fonctions to read and write SVG (Scalable Vector Graphic) files to and from Matlab structures

44 Downloads

Updated 20 Mar 2018

GitHub view license on GitHub

This contains Matlab code to load the content of an SVG file into a simple Matlab structure (loadSVG.m), to save a Matlab structure containing polygons and images into a SVG file (saveSVG.m) and a function to display this structure in a Matlab figure (plotSVG.m). It is possible to edit the polygons interactively within a matlab figure using plotSVG with true as the second argument. This code handles polygons but not bezier curves yet.

Comments and Ratings (5)

Jorge B

This code is error. after downloading it and executing it gives error:

Undefined function or variable 'strip'.

Guillaume

@misrak, @authors, as discussed in https://www.mathworks.com/matlabcentral/answers/434444 , the cause of the error is a bug in the code. All instances of length(imagesXml) should be replaced by imagesXml.getLength (lines 40 and 41). As it is because of the bug, the assumes that there is always one image regardless of the actual count. If there is none, then the code tries to getAttribute on an empty matrix.

While we're at it, I would recommend preinitialisin image as a struct, particularly as it's shadowing the built-in image. So

image = struct;
image.x = str2double(char(item.getAttribute('x')));

I would also recommend fixing all the mlint warnings (missing terminating semicolons, str2double instead of str2num particularly as the text comes from unverified user input, etc.)

Guillaume

I would also recommend fixing all the mlint warnings (missing terminating semicolons, str2double instead of str2num particularly as the text comes from unverified user input, etc.)

Hey Marrtin, thanks you so much for this amazing function! I am having problem when loading an svg file though. Your example worked with the 'building.svg', but it doens't work when I try to import my own svg file... Here is the error code..

>> example
Dot indexing is not supported for variables of this type.

Error in loadSVG (line 43)
image.x=str2num(char(item.getAttribute('x')));

Error in example (line 1)
svg=loadSVG('drawing.svg');

>>

it would be nice, if u could help me with that...

Thank you so much!

Updates

1.0.0.0

fix typo

1.0.0.0

improving description

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux