MATLAB Answers

How do I add an Excel spreadsheet as part of my Matlab UI?

19 views (last 30 days)
steve
steve on 13 Feb 2020
Commented: steve on 14 Feb 2020
I'm looking to add an Excel interface as part of my Matlab User Interface, as if Excel was embedded in a portion of the UI. I want the user to be able to add/remove/edit entries in an Excel OLE object, and allow all the normal Excel formulas and equations, referencing other cells in the table, etc. I'm currently using Matlab 2017 and using GUIDE for the rest of my GUI. The data that is entered in the spreadsheet will be used to dictate how my program will run.
I'm currently using the 'Table' object from the Matlab GUIDE to get the look-and-feel, but unfortunately, didn't realize that I can't do formulas like Excel, or reference other entries in the table. Since I ultimately need something that is essentially Excel, I don't know if I just need to embed an Excel OLE object into the UI. (And I don't quite know how to do that.)
Thanks,

  0 Comments

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 13 Feb 2020
The only way you could do this is with an activex control but my understanding is that microsoft no longer offer an activex spreadsheet control. It was part of OWC which was discontinued with office 2007. Furthermore, the controls were most likely 32-bit so it's very unlikely that they'd work in 64-bit matlab even if you could find them.
Perhaps, instead of trying to embed excel in matlab, you could do the opposite by embedded matlab into an excel spreasheet (using matlab as an activex server, that you'd call via VBA from excel).
Otherwise, the only thing I can think of is to start excel from your GUI (via actxserver) and let the user interact directly with excel with your GUI listening to excel events and reacting appropriately. It's likely to be clunky.
...or give up on the idea, I'm afraid. Note that GUIDE, activex (and java) is likely to go away in a future matlab version.

  8 Comments

Show 5 older comments
steve
steve on 14 Feb 2020
Thanks Guillaume, IA's demo runs with your suggested changes. (It really helps to RTFM....) This is definitely a step in the right direction, to be able to make and parse an Excel file.
So, what it looks like I would have to do is, have my user make changes in Excel, then use Matlab to slurp in those parameters, and run my program with them. If I need to make changes to the Excel parameters (say if I'm running Monte Carlo analysis over a particular parameter), my scripts would have to produce an Excel file, reparse the information (just in case some Excel formulas need to be updated), and run my program.
Does that sound about right?
Guillaume
Guillaume on 14 Feb 2020
That's one option if that's acceptable to you. In that case you don't need to use actxserver or anything like this, just read and write excel file and let the user deal with excel. In modern matlab versions, you'd be using writetable and readtable or writematrix and readmatrix to write/read excel file. And in older version, unless you're using a very old version you wouldn't use xlswrite1, the built-in xlswrite works just as well.
What I was suggesting and showed as an example is a lot more complex (and a lot more work!) in that your GUI starts excel, loads/creates the required spreadsheet(s), let the user interact with excel but monitor what the user is doing (via COM events) to react accordingly. For example, my little script above reacts to the user changing the selection in any spreadsheet, creating a new spreadsheet or editing the content of the first spreadsheet. Or at least, it will after I fix the typos...
steve
steve on 14 Feb 2020
OK, so this isn't going to be a clean interface like I would've hoped. Just like Guillaume mentioned, this is going to be clunky.
As a workaround (or plan B), would someone be able to help me figure out what logic I'll need to add to my uitable callbacks, such that I'll be able to have someone reference the values that are in the same table.
For example, in Excel, it's quite common to type '=A3*10.4' in a random cell, and it will take the value of Cell A3, multiply it by 10.4, and update the current cell's value with that result. Is there a way to do something like that?
I'd think I'd have to do soemthing clever witht he CellEditCallback and CellSelectionCallback for my table to handle that. If I were to do that wtih my current uitable, it just stops editing the cell I was on, and start editing the new cell I clicked.

Sign in to comment.

Sign in to answer this question.

Products