How do I generate executable code from imported data?

I have an xlsx file with various data for the calculation I'd like to conduct with my Matlab code. This file also contains the relevant formulas. Is there a way to import those formulas from xlsx (having them as a string) and convert them to normal code thats executable?

5 comentarios

Dyuman Joshi
Dyuman Joshi el 27 de Nov. de 2023
Editada: Dyuman Joshi el 28 de Nov. de 2023
Can it be done? Yes. Is it recommended? No.
Though, you could define the formulae in the syntax of function handles and utilize str2func to convert them to anonymous functions, and use accordingly.
That is a good approach, thank you. However, the str2func command does not accept variables from outside the function. As I have pre-defined variables, there is an issue with the function
Can you show what the formulas look like? It will even better if you could attach the excel file.
Johannes
Johannes el 27 de Nov. de 2023
Editada: Johannes el 27 de Nov. de 2023
I made a test script for the function. My minimum code example looks like this:
clear all
close all
clc
Flushmatrix = readtable('Spülmatrix2.xlsx','PreserveVariableNames',true);
%%
psat = 1.5;
p_fmin = psat+1;
v = 330;
%%
a = Flushmatrix(1,9);
a = string(table2cell(a));
%%
fh = str2func(a)
fh(v, psat, p_fmin)
The xlsx file contains the variables and functions. I attached it to this comment. The problem is that the function is not callable like this. If I try to run it with my preset variables, it gives this error:
Undefined function
'((table2array(Flushmatrix(1,3))*(v/1000)+table2array(Flushmatrix(1,4)))*log(psat)+(table2array(Flushmatrix(1,5))*v+table2array(Flushmatrix(1,6))))/1000'
for input arguments of type 'double'.
Error in str2func_Test (line 16)
fh(v, psat, p_fmin)
Sorry, I was away from my PC due to some other work. Please check my answer below.

Iniciar sesión para comentar.

 Respuesta aceptada

You need to add the @(list_of_independent_variables) before the formulae.
Flushmatrix = readtable('Spülmatrix2.xlsx','PreserveVariableNames',true)
Flushmatrix = 3×9 table
Spülen Produkt K1 [s/mL] K2 [s] K3 [s/mL] K4 [s] K5 K6 Funktion ____________ ________ _________ _________ _________ ________ _____ ____ _____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ {'standard'} {'Bier'} -0.23643 -0.040387 0.7205 0.039548 NaN NaN {'@(Flushmatrix, v, psat) ((table2array(Flushmatrix(1,3))*(v/1000)+table2array(Flushmatrix(1,4)))*log(psat)+(table2array(Flushmatrix(1,5))*v+table2array(Flushmatrix(1,6))))/1000'' } {'standard'} {'CSD' } -0.17236 -0.038639 0.47643 0.037187 NaN NaN {'@(Flushmatrix, v, psat) ((table2array(Flushmatrix(2,3))*(v/1000)+table2array(Flushmatrix(2,4)))*log(psat)+(table2array(Flushmatrix(2,5))*v+table2array(Flushmatrix(2,6))))/1000'' } {'sanft' } {'Bier'} 175.4 -1666 -345.6 3823 3.032 -305 {'@(Flushmatrix, v, psat, p_fmin) ((table2array(Flushmatrix(3,3))*p_fmin+table2array(Flushmatrix(3,4)))*(v/1000)^2+(table2array(Flushmatrix(3,5))*p_fmin+table2array(Flushmatrix(3,6)))*(v/1000)+(table2array(Flushmatrix(3,7))*p_fmin+table2array(Flushmatrix(3,8))))/1000'}
%values for variables
psat = 1.5;
p_fmin = psat+1;
v = 330;
%Value from the formula copied and pasted
((table2array(Flushmatrix(1,3))*(v/1000)+table2array(Flushmatrix(1,4)))*log(psat)+(table2array(Flushmatrix(1,5))*v+table2array(Flushmatrix(1,6))))/1000
ans = 0.2378
%formula from the table read
a = Flushmatrix(1,9);
a = string(table2cell(a))
a = "@(Flushmatrix, v, psat) ((table2array(Flushmatrix(1,3))*(v/1000)+table2array(Flushmatrix(1,4)))*log(psat)+(table2array(Flushmatrix(1,5))*v+table2array(Flushmatrix(1,6))))/1000'"
%convert the string to a function handle
fh = str2func(a)
fh = function_handle with value:
@(Flushmatrix,v,psat)((table2array(Flushmatrix(1,3))*(v/1000)+table2array(Flushmatrix(1,4)))*log(psat)+(table2array(Flushmatrix(1,5))*v+table2array(Flushmatrix(1,6))))/1000'
%corresponding value
fh(Flushmatrix, v, psat)
ans = 0.2378

3 comentarios

Though, I am wondering, can't you just copy and paste the formulas from the excel file to MATLAB?
Thank you so much for the help. We are working with the Matlab App Designer, so in the long term this could maybe be implemented in an app. There might be users with no Matlab license, so it`s more convenient to just change the xlsx file when one needs to change the formulae or coefficients.
I see.
Also, you can modify this lines -
a = Flushmatrix(1,9);
a = string(table2cell(a));
fh = str2func(a);
to
fh = str2func(Flushmatrix{1,9})
For more info - Access Data in Tables

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2023a

Preguntada:

el 27 de Nov. de 2023

Comentada:

el 29 de Nov. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by