get uiTable data when using it in a function

3 views (last 30 days)
Hey,
I want to load a table in a uiTable, so the user can make changes. When closing this, I want to get the latest version of the table and save it in the workspace of the function, but it seems that the UI table is always made in the Base workspace?
I tried with th ecell editCalback but it can't find the 'uit' object,....
Then a second try was when closing the function:
function [table] = testTable()
table = readtable([[pwd,'\Configs\templates'],'\','vc_LYVP_template.csv']);
fig = uifigure;
uit = uitable(fig,'data',table);
uit.ColumnEditable = [false,true,false,false,false];
set(fig,'CloseRequestFcn',@myCloseFcn)
uiwait(fig)
end
function myCloseFcn(~,~)
table = get(uit,'data');
assignin('caller','table',table)
end
But I still get errors that ity can't find uit, or the data is saved in the base workspace in stead of the function
Does anyone have any idea on how to solve this?

Accepted Answer

Rik
Rik on 19 Jan 2022
Edited: Rik on 19 Jan 2022
For general advice and examples for how to create a GUI, have look at this thread.
If you want to stick with this setup you will have to make sure all variables exist in a share workspace. The easiest way would be to make your closing function a nested function, which would allow it to access variables in the parent function:
function [table] = testTable()
table = readtable([[pwd,'\Configs\templates'],'\','vc_LYVP_template.csv']);
fig = uifigure;
uit = uitable(fig,'data',table);
uit.ColumnEditable = [false,true,false,false,false];
set(fig,'CloseRequestFcn',@myCloseFcn)
uiwait(fig)
function myCloseFcn(hObject,~)
table = get(uit,'data');
delete(hObject) % something like gcbf will also work
end
end
I think this will work as intended.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!