Best way to build a database-like variable?

조회 수: 4 (최근 30일)
Sven Schoeberichts
Sven Schoeberichts 2011년 10월 19일
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

채택된 답변

Sven Schoeberichts
Sven Schoeberichts 2011년 10월 19일
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
Artik Crazy
Artik Crazy 2011년 10월 19일
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 2011년 10월 19일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Artik Crazy
Artik Crazy 2011년 10월 19일
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.

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by