Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-14, 20:46   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何在Matlab中将单元格行中的值转换为向量

我想要第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行中给出的和值。

(0.707107, 0.707107, 0) 0.707107 0.707107 0 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 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 11781 11564 11349 11136 10925 10716 10509 10304 10101 9900 9701 9504 9309 9116 8925 8736 8549 8364

回答:

除了读取整个文件的内容并进行解析之外,还可以使用TEXTSCAN从文件中读取所需的值:

fid = fopen('experimental_var.dat','r'); %# Open the file x = textscan(fid,'%f','HeaderLines',4,... %# Ignore 4 header lines 'Delimiter',' ',... %# Add and as delimiters 'MultipleDelimsAsOne',true); %# Combine delimiters into one y = textscan(fid,'%f','Delimiter',' ',... %# Add and 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

更多&回答...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 20:42


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.