table in GUI --> data to matrix, expandable array

3 views (last 30 days)
Hey Guys,
i'm just working on a GUI and until now i just an extra dat-file for the input of my values.. but now i would like to use the table in the GUI!
So my questions are:
How do get the data input from the editable table in the GUI into a matrix i can use in my m-file?
and how can i add more rows to the table (from the UI) and remove unused ones? (would be even better if this could happen automatically..)
Hope you can give me a hand i just can not find a way,...
Thx
Mwafrika

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 20 Jul 2011
First, create an example table.
h=uitable('data',magic(5),'columneditable',true,'RearrangeableColumns','on');
Then, make any arbitrary changes on the data. If you want to get the new data in your .m file, make sure you have access to the handle h.
data=get(h,'data');
If you want to add or remove rows, you can add a button say "add row" and then in the callback function, do something like this.
data=get(h,'data');
data(end+1,:)=0;
set(h,'data',data');
To remove a row, if you could have a way to specify the row number r, for example r=2, you can do.
data=get(h,'data');
data(2,:)=[];
set(h,'data',data);
  2 Comments
Walter Roberson
Walter Roberson on 20 Jul 2011
Small change: "data" will be a cell array, so you cannot set dat(end+1,:) to the numeric value 0. You can set it to {0} or {} or as appropriate.
Fangjun Jiang
Fangjun Jiang on 20 Jul 2011
I am not sure. When I use h=uitable('data',magic(5)), class(get(h,'data')) is double. So it is the other way around. But, in general, yes, should look at the data class to do things differently.

Sign in to comment.

More Answers (2)

Mwafrika
Mwafrika on 26 Jul 2011
Thx Fangjun Jiang,
i did it now in a little different way...
i linked my dat file to the table in the GUI using get and csvwrite.. the adding and removing of rows with your code worked fine in combination with this way!
THX again
Mwafrika

Guilherne
Guilherne on 2 Oct 2011
It was very helpful, thanks!

Community Treasure Hunt

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

Start Hunting!