downloading table data to a file in GUI

1 次查看(过去 30 天)
I am trying to get the data from the table in GUI and I am getting the same error as mentioned above "Undefined function 'write' for input arguments of type 'double'."
My code goes here:
T = get(handles.uitable1, 'Data');
fullpathAndFilename = '/home/mytable.txt';
writetable(T, fullpathAndFilename);
The items in the table is shown below:
Please Help me. Thanks.

采纳的回答

Walter Roberson
Walter Roberson 2018-7-11
uitable() do not store table() objects.
The following code looks at the column names to create variables for the table objects. It does not assume that names are already present ('ColumnName' 'numbered' is valid). It does not assume that there are enough column names provided, and does not assume that any entries provided are valid unique variable names. If more headers are present than data columns then it extends the data columns.
Most of the work below is to ensure that valid headers of the right size are put in place. The actual work of creating the table and writing it is pretty short, so if you already know that your headers are valid and the right size then potentially you could make this code significantly shorter.
fullpathAndFilename = '/home/mytable.txt';
data = get(handles.uitable1, 'Data');
if ~iscell(data); data = num2cell(data); end
nc = size(data, 2);
%caution: orientation of ColumnName cell vector might not be what last set,
%MATLAB might have changed it
cn = get(handles.uitable1, 'ColumnName');
if isempty(cn) || ~iscell(cn)
cn = cell(1, nc);
end
nch = length(cn);
if nc < nch
data{end, nch} = []; %and all intermediate items also get []
nc = nch;
elseif nch < nc
cn{nc} = []; %and all intermediate items also get []
nch = nc;
end
mask = cellfun(@isempty, cn);
cn(mask) = sprintfc('var%d', find(mask)); %fill empty column names
cn = matlab.lang.makeUniqueStrings( matlab.lang.makeValidName(cn, 'ReplacementStyle', 'hex') );
T = cell2table(data, 'VariableNames', cn);
writetable(T, fullpathAndFilename);
  5 个评论
Walter Roberson
Walter Roberson 2018-7-11
If you want a text file with no headers, then there is no point in using a table() object.
fullpathAndFilename = '/home/mytable.txt';
data = get(handles.uitable1, 'Data');
save(fullpathAndFilename, 'data', '-ascii')
Vinayak Appasaheb Bhatte
Thank you so much for your patience and answers. It worked finally the way I wanted it to be.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by