downloading table data to a file in GUI

3 ビュー (過去 30 日間)
Vinayak Appasaheb Bhatte
Vinayak Appasaheb Bhatte 2018 年 7 月 11 日
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
Vinayak Appasaheb Bhatte 2018 年 7 月 11 日
Thank you so much for your patience and answers. It worked finally the way I wanted it to be.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace 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