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일
편집: Stephen23 2017년 11월 3일
"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"
Trying to evaluate that string will be waste of your time and MATLAB's time. You would be much better off writing the data to a standard CSV file, so it looks like this:
1,2,3
4,5,6
7,8,9
And then simply importing that into MATLAB using csvread:
A = csvread(...);
Using any of the standard file input-output formats and functions will be a lot simpler, waste less time, and be more efficient than what you are trying to do.
Jakob
Jakob 2017년 11월 3일
편집: Jakob 2017년 11월 3일
Thanks a lot, yeah that sound way easier! But this was just an example, the matrices are way more complex containing functions for the matrix-entries. Does that still work with entries like e.g.:
-l2(1)*(G(2,5)*sin(t(1)-t(3))
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일

0 개 추천

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개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

질문:

2017년 11월 3일

편집:

2017년 11월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by