textから小数点を​含む数値を取り出すに​はどうすればよいでし​ょうか

7 vues (au cours des 30 derniers jours)
Dai
Dai le 31 Juil 2022
Commenté : Dai le 1 Août 2022
SVGのファイルをfilereadを用いてtextに変換し、そこから座標情報を取り出したいのですが、上手くいきません。
extract(Text, digitsPattern) を用いると、小数点は数値として認識されないようで、38.24が38と24として取り出されてしまいます。
とりあえず整数部分と小数部分に分けて取り出し、行列をreshapeを用いて上手く並べた後、num2doubleで数値にし、足し合わせることも検討しましたが、
小数点以下が.00の数値ですと、text上、小数点以下は表示されない、つまり、17.00は17と表示されるため、これもまたうまくreshapeできず解決しません。
以下に、
A=fileread(filename.svg)
extractbetween(A,'文字列','文字列')を使用して抽出したテキストをTextの中に入れた後からのコードを示しますのでよろしくお願いいたします。
Text内には
<rect x="1628.79" y="2086.67" class="st4" width="39.69" height="39.69"/>
<rect x="1631.63" y="2121.57" class="st4" width="39.69" height="39.69"/>
<rect x="1646.75" y="2193.81" class="st4" width="39.69" height="39.69"/>
<rect x="1729" y="2333.73" class="st4" width="39.69" height="39.69"/>
<rect x="1551.15" y="1804.36" class="st4" width="39.69" height="39.69"/>
<rect x="1384.64" y="1235.46" class="st4" width="39.69" height="39.69"/>
<rect x="1720.75" y="772.62" class="st4" width="39.69" height="39.69"/>
が入っています
Text
NumC = extract(Text,digitsPattern);
NumV = str2double(NumC);
これだと、
NumC =
[1628 79 2086 67...
と返ってきてしまい、また、4行目の最も左の値が整数であるために、NumVをreshapeすることも困難です
何か解決策はありますか
  1 commentaire
Atsushi Ueno
Atsushi Ueno le 31 Juil 2022
下記に質問に対する回答をさせて頂きましたが、SVGファイルのフォーマットはXMLなので、XMLパーサで抽出すればもっと楽に、かつパラメータ名を含めて識別し構造体に読み込む事が出来るので良いと思います。

Connectez-vous pour commenter.

Réponse acceptée

Atsushi Ueno
Atsushi Ueno le 31 Juil 2022
二重引用符で囲まれた小数点数(または整数)をパターンにして抽出してみました。
二重引用符も含めて抽出した後で削除していますが、正規表現の「先/後読み」が出来ればこの必要はなくなります。
Text = "<rect x=""1628.79"" y=""2086.67"" class=""st4"" width=""39.69"" height=""39.69""/>" + ...
"<rect x=""1631.63"" y=""2121.57"" class=""st4"" width=""39.69"" height=""39.69""/>" + ...
"<rect x=""1646.75"" y=""2193.81"" class=""st4"" width=""39.69"" height=""39.69""/>" + ...
"<rect x=""1729"" y=""2333.73"" class=""st4"" width=""39.69"" height=""39.69""/>" + ...
"<rect x=""1551.15"" y=""1804.36"" class=""st4"" width=""39.69"" height=""39.69""/>" + ...
"<rect x=""1384.64"" y=""1235.46"" class=""st4"" width=""39.69"" height=""39.69""/>" + ...
"<rect x=""1720.75"" y=""772.62"" class=""st4"" width=""39.69"" height=""39.69""/>";
floatdigitsPattern = """" + digitsPattern + asManyOfPattern("." + digitsPattern) + """";
NumC = extract(Text,floatdigitsPattern);
NumV = str2double(erase(NumC,""""));
reshape(NumV,4,[])'
ans = 7×4
1.0e+03 * 1.6288 2.0867 0.0397 0.0397 1.6316 2.1216 0.0397 0.0397 1.6467 2.1938 0.0397 0.0397 1.7290 2.3337 0.0397 0.0397 1.5512 1.8044 0.0397 0.0397 1.3846 1.2355 0.0397 0.0397 1.7208 0.7726 0.0397 0.0397
  1 commentaire
Dai
Dai le 1 Août 2022
無事解決いたしました。
ありがとうございます。

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 文字と文字列 dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!