Build Matrix from Text file or String

9 次查看(过去 30 天)
Hey Guys, I'm working with Mathematica and MatLab and I just wrote a function, which translates the results (matrices and vectors) I get in Mathematica (saved as a .txt-file) into the syntax I'm using in my MatLab script. The output of this function is a char-array and I also save it as a .txt-file. Which looks like that:
'[1 2 3;4 5 6;7 8 9];'
And I want to have this as an actual asignment for the matrix-elements, so something like that:
A=[1 2 3;4 5 6;7 8 9];
Is there a way to convert this character array into an actual MatLab command to asign the matrix-elements?
  4 个评论
Stephen23
Stephen23 2017-11-3
@Jakob: you are trying to use MATLAB as a parser, which is rarely efficient. It may be simpler to create those files to contain valid MATLAB code, and then just call the script.
Jakob
Jakob 2017-11-3
编辑:Jakob 2017-11-3
Thanks Stephen, but I don't really understand what you mean. Just to explain, why I need this... I have to calculate the matrix entries new in each iteration, and the matrix entries should be the description for the calculation. So can I use characters in the CSV-file? The equations will then probably be handeled like strings.

请先登录,再进行评论。

采纳的回答

KSSV
KSSV 2017-11-3
A = '[1 2 3;4 5 6;7 8 9];'
B = str2num(A)
  1 个评论
Stephen23
Stephen23 2017-11-3
This works, but is very inefficient and relies on eval. The best solution would be to import that data properly by writing a standard file format and import that as numeric data.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by