matlab中的字符编程
我有包含字符 time1、time2、time3 直到 time60 的数据。这意味着每次都有自己的结果,例如time1=70
、time2=56
等等...如何将这些数据排列在矩阵中而不需要手动排列他们是:
time=[time1 time2 time3 time4 time5.......time60].
除了上述步骤之外,我不知道。上述步骤需要花费更多时间才能输入 60 个数据。有没有最简单的方法来排列这些数据?
I have data with character time1, time2,time3 until time60. Which means each time have their own result,for example time1=70
,time2=56
and etc.... how to arrange this data in matrix without need to manually arrange them as:
time=[time1 time2 time3 time4 time5.......time60].
I have no idea aside from above step. The above step is taking more time in order to type until 60 data. Is there any easiest way to arrange these data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 for 循环内使用
eval
命令。You can use the
eval
command inside a for-loop.您可以直接执行此操作,无需循环,如下所示。
但是,将来,请尝试不要将变量命名为
time1
、time2
等。相反,请考虑将值存储在向量中如时间(1)=...
、时间(2)=...
。这使您的代码更清晰,更少的变量使您的工作区变得混乱,可以利用 MATLAB 真正擅长的矢量化运算(这取决于您使用它做什么......)以及如果您需要将值保存到MAT
文件,您只需要保存一个变量而不是 60 个。我还建议尽可能不要使用
eval
,并且仅在无法避免的情况下使用(例如,与其他人一起工作代码/数据)。You can do it straightforwardly without a loop as
However, in future, try not to name your variables as
time1
,time2
, etc. Instead, consider storing the values in a vector astime(1)=...
,time(2)=...
. This makes your code cleaner, fewer variables cluttering your workspace, can take advantage of vectorized operations which MATLAB is really good at (this depends on what you do with it though...) and if you need to save the values to aMAT
file, you only need to save a single variable instead of 60.I would also recommend against using
eval
as much as possible, and only in cases where it can't be avoided (e.g., working with someone else's code/data).