如何在 MATLAB 中读取该文件?

发布于 2024-09-06 03:49:05 字数 278 浏览 2 评论 0原文

我有一个名为 data.dat 的文件,其中包含以下内容:

我的名字是埃利亚斯 123

这是一本书 123.450

我父亲的名字-2.34e+05

我想将此文件加载到 MATLAB 中并获取以下数据作为输出:

a = 123
b = 123.450
c = -2.34e+05
name = 'elyas'

但我不知道该怎么做。有什么建议吗?

I have a file named data.dat with the following contents:

my name is elyas 123

this is a book 123.450

my father name -2.34e+05

I want load this file into MATLAB and get the following data as output:

a = 123
b = 123.450
c = -2.34e+05
name = 'elyas'

But I don't know how to do this. Any suggestions?

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

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

发布评论

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

评论(2

计㈡愣 2024-09-13 03:49:05

这是一种使用 TEXTSCAN 进行阅读的方法3 行中的每一行:

fid = fopen('data.dat','rt');                 %# Open the file
data = textscan(fid,'%*s %*s %*s %s %f',1);   %# Read the first line, ignoring
                                              %#   the first 3 strings
name = data{1}{1};                            %# Get the string 'name'
a = data{2};                                  %# Get the value for 'a'
data = textscan(fid,'%*s %*s %*s %*s %f',1);  %# Read the second line, ignoring
                                              %#   the first 4 strings
b = data{1};                                  %# Get the value for 'b'
data = textscan(fid,'%*s %*s %*s %f',1);      %# Read the third line, ignoring
                                              %#   the first 3 strings
c = data{1};                                  %# Get the value for 'c'
fclose(fid);                                  %# Close the file

Here's one way to do it using TEXTSCAN to read each of the 3 lines:

fid = fopen('data.dat','rt');                 %# Open the file
data = textscan(fid,'%*s %*s %*s %s %f',1);   %# Read the first line, ignoring
                                              %#   the first 3 strings
name = data{1}{1};                            %# Get the string 'name'
a = data{2};                                  %# Get the value for 'a'
data = textscan(fid,'%*s %*s %*s %*s %f',1);  %# Read the second line, ignoring
                                              %#   the first 4 strings
b = data{1};                                  %# Get the value for 'b'
data = textscan(fid,'%*s %*s %*s %f',1);      %# Read the third line, ignoring
                                              %#   the first 3 strings
c = data{1};                                  %# Get the value for 'c'
fclose(fid);                                  %# Close the file
请恋爱 2024-09-13 03:49:05

您可以尝试textscan

You could try textscan.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文