downloading table data to a file in GUI

조회 수: 1 (최근 30일)
Vinayak Appasaheb Bhatte
Vinayak Appasaheb Bhatte 2018년 7월 11일
댓글: 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 CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by