【问题标题】:Store integers from a text file into variable arrays将文本文件中的整数存储到变量数组中
【发布时间】:2019-10-01 12:59:11
【问题描述】:

我需要使用一些文本文件在 MATLAB 中绘制 x、y 的坐标。 我在使用 for 循环读取它时遇到问题。

我可以用 Python 计算它,但我需要帮助才能在 MATLAB 中转换它。

这是 Python 中的一些代码

   file = open("6.txt", "r")
   x = []
   y = []
   z = []
   for i in file.readlines()[::]:
       if i[0] == "(":
           jam = i.strip('()').split(",")
           x.append(float(jam[0]))
           y.append(float(jam[1]))
           jam = i.strip('()\n').split(",")
           z.append(float(jam[2]))
   '''

但在 Matlab 中,我最初是从这段代码开始的

    fileID = fopen('1.txt', 'r+');
    formatSpec = '%s';
    for i = fscanf(fileID, '%s')[::]

在 Python 中的结果是

x = [1.154545 1.265648 ..... 1.56849] 
Y = [1.0 1.5655 1.61662 ..... 1.0] 

【问题讨论】:

  • 您是否尝试过使用导入向导?有了这样一个简单的文件,使用导入向导生成一个脚本来导入这些数据是很简单的。

标签: matlab for-loop if-statement


【解决方案1】:

你可以试试这样的:

content=readmatrix('1.txt') %read the entire content as a matrix
x=content(:1) %extract the first column
y=content(:2) %extract the second column
plot(x,y) %plot the data

我现在无法测试上面的代码,这就是我评论我写的每一行的原因。但算法仍然存在。

【讨论】:

  • 它是一个嘈杂的文本数据,所以我想对其进行循环以对其进行预处理
  • 文件就像notevook 1 No. of Stroke:28 (0.24049437046051025, 0.22948816418647766, 1.0) (0.24160410463809967, 0.22948816418647766, 1.0)
  • 您也可以在提取和绘图之间尽可能多地预处理xy
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 2012-08-29
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多