What is the easiest way to import a text matrix into Matlab?

HI,
I exported a symbolic matrix from Maple as a text file, and now want to import it to Matlab.
What is the easiest way to import it to Matlab?
Is there any special ready to use function to read it and put in a matrix?
Here I have put two line of that text file, which each line represents each raw of the (2*6) matrix.
x*y*z 1/2*tau[2] sin(x*y*z) ln(x*y*z) tau*exp(x*y*z) sin(x+y)+cos(x+y)
x^2+1 x^2+1/sin(x*y*z) 2*exp(y) tau diff(f(x,y,z),x) int(f(x,y,z),x)

 採用された回答

Walter Roberson
Walter Roberson 2022 年 9 月 10 日

0 投票

readcell(FileNameGoesHere, 'delimiter', '\t')
or
readtable(FileNameGoesHere, 'delimiter', '\t', 'readvariablenames', false)
Question:
Have you considered using Maple's CodeGeneration[Matlab] function ?

7 件のコメント

Mehdi
Mehdi 2022 年 9 月 10 日
編集済み: Mehdi 2022 年 9 月 10 日
It worked by putting the name between "". Then converting to cell to matrix, faced error
>>A= readcell("ma.txt", 'delimiter', '\t')
A =
2×6 cell array
{'x*y*z'} {'1/2*tau[2]' } {'sin(x*y*z)'} {'ln(x*y*z)'} {'tau[1]*exp(x*y*z)'} {'sin(x+y)+cos(x+y)'}
{'x^2+1'} {'x^2+1/sin(x*y*z)'} {'2*exp(y)' } {'tau[1]' } {'diff(f(x,y,z),x)' } {'int(f(x,y,z),x)' }
>> B=cell2mat(A)
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 78)
m = cat(1,m{:});
Have you considered using Maple's CodeGeneration[Matlab] function ?
yes, since my matrix has some variables such as x[1], y[8], ... this command faced error. Also this function creates codes only, not m file. I have to copy and paste them manually which for big matrices is not a good idea.
Walter Roberson
Walter Roberson 2022 年 9 月 10 日
B=cell2mat(A)
What are you hoping that the output of that command would give you?
Mehdi
Mehdi 2022 年 9 月 10 日
Maple face error: Error, (in codegen/makeproc) optional arguments must be equations [x[1], x[2]]
No way to change the cell to matrix?
Walter Roberson
Walter Roberson 2022 年 9 月 10 日
What datetype were you hoping for the array. What size() were you hoping for the array?
Mehdi
Mehdi 2022 年 9 月 10 日
編集済み: Mehdi 2022 年 9 月 10 日
I am looking for 2*6 symbolic matrix exactly similar to what was at Maple.
[x*y*z, 1/2*tau, sin(x*y*z), ln(x*y*z), tau*exp(x*y*z), sin(x+y)+cos(x+y);
x^2+1, x^2+1/sin(x*y*z), 2*exp(y), tau, diff(f(x,y,z),x), int(f(x,y,z),x)];
Walter Roberson
Walter Roberson 2022 年 9 月 10 日
You would need to use str2sym() to convert the entries to symbolic.
However, your input includes '1/2*tau[2]' which is not valid MATLAB symbolic expression.
It also includes ln(x*y*z) -- remember that the MATLAB log is log() not ln()

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by