Best way to build a database-like variable?

3 views (last 30 days)
Hi all,
I need to set up a sort of database (within a variable) in Matlab. It needs to have column titles at the top, and 2 cells of strings at the beginning of each row. I better visualize it:
Example:
'fid' 'fname' 'beats' 'param1' 'param2' '...' 'param66'
'B10A130' 'A130.xls' 239 3.3462 6.3002 ... 1.0063
'B10A131' 'A131.xls' 302 4.2465 6.4051 ... 0.9354
'B10A132' 'A132.xls' 287 5.6232 5.9402 ... 0.8531
Data will be entered one value at a time, in a loop, so not one line at a time. This file will be filled with data over a longer period of time, so it will be saved and loaded a number of times.
What is the best variable to choose? I think a cell, right? Or a structure of some kind?
The data needs to be extracted too, but a column at a time, so when I would need all beats, I should want to enter:
x = database(2:end, 3)
Would that work with a cell type? I tried it on a test-cell but I had to use {} and I got a bunch of seperate ANS, instead of one array of numbers.
So what do you guys reckon, what would be best for me to set up this project? I'm using MATLAB 2010b
Thanks in advance,
Sven

Accepted Answer

Sven Schoeberichts
Sven Schoeberichts on 19 Oct 2011
Thanks for your input Artik,
I spent some time testing if a dataset would fit my needs, and I guess it does!
I am stuck with an other question now.
In my example above, it seems I can't enter strings longer or shorter than the first entry. So my 'fname' is called "A130.xls", which is 8 characters. If I try to enter a string longer or shorter than 8 characters, it gives me the error: ??? Error using ==> dataset.subsasgn at 561 Subscripted assignment dimension mismatch.
Am I doing something wrong in putting data into the dataset, or is this normal? And can it be bypassed any way?
  2 Comments
Artik Crazy
Artik Crazy on 19 Oct 2011
You will have to put the strings into cells before.
Like this:
Test=dataset({cell(3,1), 'Name'}, {zeros(3,1), 'Age'})
Test.Name(3)={'Jack'};
Test.Name(2)={'John'};
Test.Name(1)={'Jim'};
But when you type Test.Name it will give you:
Test.Name
ans =
'Jim'
'John'
'Jack'
Sven Schoeberichts
Sven Schoeberichts on 19 Oct 2011
I searched everything on Google until I found a script which used cellstr(), which did the trick, just like your solution.
Thanks for the help!

Sign in to comment.

More Answers (1)

Artik Crazy
Artik Crazy on 19 Oct 2011
Hi, I find a dataset array to be quite useful in such tasks. It can be used as matrix and as a structure in the same time, and has a join function, that is particularly useful function in a "database like" constructions. This array is part of Statistical Toolbox.

Community Treasure Hunt

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

Start Hunting!