如何在Matlab中将单元格行中的值转换为向量

发布于 2024-10-24 02:39:49 字数 1044 浏览 4 评论 0原文

我想要第 3 行和第 4 行(=x 和 y)的值,如下面的数据所示。这是我正在尝试转换的 Matlab 代码:

fid = fopen('experimental_var.dat');

%# read it into one big array, row by row
fileContents = textscan(fid,'%s','Delimiter','\n');
fileContents = fileContents{1};
fclose(fid); %# don't forget to close the file again

x=fileContents(5);
y=fileContents(6);

有人可以帮助提取第 3 行和第 4 行中给出的 的值吗?下面粘贴的数据文件。

    <title>(0.707107, 0.707107, 0)</title>
    <direction>0.707107 0.707107 0 </direction>
    <x>1.41421 2.82843 4.24264 5.65685 7.07107 8.48528 9.89949 11.3137 12.7279 14.1421 15.5563 16.9706 18.3848 19.799 21.2132 22.6274 24.0416 25.4558 </x>
    <y>2.08978 3.09925 4.80142 7.05703 9.66079 12.56 15.5897 18.6189 21.5112 24.1628 26.4319 28.2853 29.7518 30.7296 31.3153 31.5626 31.5141 31.2843 </y>
    <pairs>11781 11564 11349 11136 10925 10716 10509 10304 10101 9900 9701 9504 9309 9116 8925 8736 8549 8364     </pairs>

I want the values of row 3 and 4 (=x and y) as shown in the data below. This is my Matlab code that I am trying to convert:

fid = fopen('experimental_var.dat');

%# read it into one big array, row by row
fileContents = textscan(fid,'%s','Delimiter','\n');
fileContents = fileContents{1};
fclose(fid); %# don't forget to close the file again

x=fileContents(5);
y=fileContents(6);

Can someone help to extract those values of <x> and <y> given in rows 3 and 4 of the data file pasted below.

    <title>(0.707107, 0.707107, 0)</title>
    <direction>0.707107 0.707107 0 </direction>
    <x>1.41421 2.82843 4.24264 5.65685 7.07107 8.48528 9.89949 11.3137 12.7279 14.1421 15.5563 16.9706 18.3848 19.799 21.2132 22.6274 24.0416 25.4558 </x>
    <y>2.08978 3.09925 4.80142 7.05703 9.66079 12.56 15.5897 18.6189 21.5112 24.1628 26.4319 28.2853 29.7518 30.7296 31.3153 31.5626 31.5141 31.2843 </y>
    <pairs>11781 11564 11349 11136 10925 10716 10509 10304 10101 9900 9701 9504 9309 9116 8925 8736 8549 8364     </pairs>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2024-10-31 02:39:49

可以使用 TEXTSCAN 仅从文件中读取所需的值:

fid = fopen('experimental_var.dat','r');       %# Open the file
x = textscan(fid,'%f','HeaderLines',4,...      %# Ignore 4 header lines
             'Delimiter',' </x>',...           %# Add <x> and </x> as delimiters
             'MultipleDelimsAsOne',true);      %# Combine delimiters into one
y = textscan(fid,'%f','Delimiter',' </y>',...  %# Add <y> and </y> as delimiters
             'MultipleDelimsAsOne',true);      %# Combine delimiters into one
fclose(fid);                                   %# Close the file
x = x{1};     %# Remove the cell array encapsulation
y = y{1};     %# Remove the cell array encapsulation

Instead of reading in the entire file contents and parsing them, it's possible to use TEXTSCAN to just read the values you want from the file:

fid = fopen('experimental_var.dat','r');       %# Open the file
x = textscan(fid,'%f','HeaderLines',4,...      %# Ignore 4 header lines
             'Delimiter',' </x>',...           %# Add <x> and </x> as delimiters
             'MultipleDelimsAsOne',true);      %# Combine delimiters into one
y = textscan(fid,'%f','Delimiter',' </y>',...  %# Add <y> and </y> as delimiters
             'MultipleDelimsAsOne',true);      %# Combine delimiters into one
fclose(fid);                                   %# Close the file
x = x{1};     %# Remove the cell array encapsulation
y = y{1};     %# Remove the cell array encapsulation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文