Matlab - Structure Arrays? Need some help on some functions

Discussion in 'Programmer's Corner' started by Judas543, Apr 10, 2010.

  1. Judas543

    Thread Starter Member

    Jan 26, 2010

    What I need help is this:
    Modify an entry in the DataBase (e.g., a value has been entered mistakenly as a number instead of a string)
    *1)Obviously I know how to do this by indexing it like calling its structure name.
    But how would I do that in a script, like without doing it to a specific name. In essense so the user chooses which item to choose to modify.

    2)Sell an item and modify the store inventory accordingly
    I have no clue to do this, I was thinking of doing rmfield(Products,'something goes here')
    However it will effect both elements in the structure, i only want to effect one item and decrease the item inventory entered to decrease by the amount. So if the original inventory was (100), if a user wanted to sell an item, it will be (99)

    If someone could enlighten me on how to do this, that would be great!

    Creation of structure =

    1) Structure for storing the products sold at a store
    2) Start the structure by entering in the script the information for the first product
    *I just entered random info
    3) SAVE structure workspace

    Products = struct('Name',[],'SKU',[],'Retail',[],'nItems',[]) %Creates Empty Structure

    Products.Name = 'Pencil Sharpener' % Product Name
    Products.SKU = '123456789' %SKU Number
    Products.Retail = 0.3000 %Product Price
    Products.nItems = 100 %Number of items in stocky

    save mProduct.mat %Saves workspace

    %function file

    function [p] = makeEntry(pName,pSKU,pRetail,pItem)
    %Makes a function that will help create a new entry

    %Our inputs are run through our function to get a new field in our

    p.Name = pName
    p.SKU = pSKU
    p.Retail = pRetail
    p.nItems = pItem


    %Main Script
    %Part of it
    %Thats to add a new entry into the Products

    case 1
    pName = input('Product Name:','s');
    pSKU =input('Product SKU:','s');
    pRetail =input('Retail Price:','s');
    pItem = input('Inventory Number:');
    x = length(Products);
    Products(x+1) = makeEntry(pName,pSKU,pRetail,pItem)
  2. johndoe45


    Jan 30, 2010
    been busy. so sorry that i can't help. have project due monday. test that took today. and test on thursday.

    plus your posts are getting harder and harder and i know nothing about words in Matlab still. i don't think we are going to cover that. we are engineers so no idea why would use strings anyway.
  3. Judas543

    Thread Starter Member

    Jan 26, 2010
    yea after long hours i finally got it :p

    Well i guess we do have to know how to manage data bases
  4. Ghar

    Active Member

    Mar 8, 2010
    I just spent a lot of time using Matlab strings as an engineer.
    I had to parse text format logs of various things from a large robot.
    I also had to do a lot of text parsing for the same project just to automate plotting the data.

    Strings are a pretty basic and useful thing to know. You can also convert a string into a function call so you can generate code with code.

    Too bad I'm too late to actually help.