MATLAB Answers

Generating and storing lists of variable lengths?

4 views (last 30 days)
Andrew M
Andrew M on 23 Jul 2021
Commented: dpb on 27 Jul 2021
I'm trying to create an app that allows you to enter information about a food recipe with variables for the name, instructions, nutrition facts (calories, fat, etc. - all stored in separate variables). These recipes would be stored in a file (xls?) and could be accessed later by a different app that would manipulate the data.
The issue I'm having - since recipes have different numbers of ingredients, I am running into trouble making a function that stores each ingredient in a way that makes them easily accessible later.
  • I am using writecell with 'WriteMode' 'append' (so that each new recipe is stored beneath the last on the spreadsheet)
  • However, since ingredients are stored in a cell, it will not allow me to store nested cells
  • To remedy this I then tried 'strjoin(ingredients)' which worked and gave one value that represented all ingredients in each recipe. However, doing it this way will make it very difficult to find the values later.
  • Another thing I tried was attaching a tag to each ingredient (e.g., ,&bananas,&oranges). My idea here was that I could then search the list for all instances of ",&" and arrive at the ingredients. However, this won't differentiate which recipe the ingredient belongs to, since the recipe list is becoming a 300+x1cell that's just a long line of these values.
Essentially where I'm stuck is that I am generating cells with varying lengths and unsure of the best way to store these into a database for later reference.
Andrew M
Andrew M on 26 Jul 2021
After taking your advice I rewrote it such that with each iteration, there are two files being saved, both structs:
One is simply a list of the names of recipes:
recipenames =
struct with fields:
recipenames: {'first recipe' 'taco'}
Second is a struct containing:
a = load('taco.mat');
ans =
struct with fields:
name: 'taco'
recipeinst: 'stir'
ingredients: {'taco' 'water'}
qty: {[2] [2]}
measure: {'piece(s)' 'tbps'}
calories: 10
totalfat: 2
cholesterol: 3
sodium: 4
carbs: 2
fiber: 3
sugar: 42
protein: 2
This approach has the advantage of allowing the app to quickly reference the "recipenames" list to see if it exists. If it does, it can then load the .mat file with the corresponding name.
The downside to this approach would be: if there are hundreds of recipes, that would mean hundreds of recipe .mat files, which I imagine could get boggy.

Sign in to comment.

Accepted Answer

Jeff Miller
Jeff Miller on 23 Jul 2021
There isn't really a good solution to this type of problem with "flat file" databases, but relational databases handle it very gracefully if you can go that route. A relational database for your situation would require (at least) three tables: one for listing the distinct recipes, one for listing the distinct ingredients, and--crucially--a third "link" table. Each row in the link table connects one recipe to one ingredient, so each recipe can have as many rows in the link table as it needs (i.e., one for each ingredient), and each ingredient has as many rows in the link table as it needs (i.e., one for each recipe that it belongs to).
dpb on 27 Jul 2021
'Cuz none of mine were actually Answer; just comments...I didn't really have an Answer; just some thoughts along the way... :)

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!