How to pass if statement string from excel to Matlab and construct a function

5 views (last 30 days)
Hey everyone,
So I need users to be able to input different possible conditional statements in excel, where then matlab reads in the conditional statement and converts it to a function that can be executed on Matlab data. I believe using a char array input from Excel would be the best way to approach this but am unsure. I could also probably build a custom parser for this but I'm hoping there is a better and/or faster approach. I was expecting to use str2func() or feval() to execute these inputs.
Here is an example of what I was thinking in terms of input in Excel:
Here each event would represent a row and there is always an initial statement to execute. However, there may or may not be conditional statements to execute. In addition it is possible for a user to want to enter more than one if statement clause as shown above. Lastly, the _n is to represent which index to reference, so DRstart_n+1 in Excel is equivalent to DRstart(n+1) in Matlab. The way I've written the conditions and outcomes can be changed and aren't set in place.
This question is most likely too specific, but any advice or insight would be greatly appreciated before tackling this.
If you have any questions please ask!
Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 12 Aug 2022
You are designing your own language representation in Excel. Unless you deliberately make it mechanically translatable to MATLAB by having all of the user input be matlab code, then you are going to need to build a parser. That can get moderately complex. See for example https://www.mathworks.com/matlabcentral/fileexchange/32769-mparser (there are other similar projects around.)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by