Insert a mathematical function in a GUI from a txt file

2 vues (au cours des 30 derniers jours)
Yannis K
Yannis K le 1 Déc 2011
I would like to insert a mathematical expression, written in a txt file in Matlab and I don't know if it is possible. For example I would like to make Matlab read from a txt file the expression:
(x^2+x)*cos(x)
or even more complicated containing sums etc.
I know this can be done if loaded from a m-file. what about .txt file?
Thanks a lot

Réponses (2)

Amith Kamath
Amith Kamath le 1 Déc 2011
This is quite interesting! I could do it this way:
Suppose a file called data has the lines:
x = 5;
y = (x^2+x)*cos(x);
Now you can read the file data as is using
z = textread('data','%s');
and with a quick conversion into a string using
y = cell2mat(z') %transpose to get the data in a row rather than a column, for z is a column of cells.
do an eval(y) to evaluate the contents of the textfile!

Walter Roberson
Walter Roberson le 1 Déc 2011

Catégories

En savoir plus sur Low-Level File I/O dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by