Saving a uitable to text file with variable number of columns

조회 수: 4 (최근 30일)
Jason
Jason 2016년 9월 26일
댓글: Jan 2016년 9월 28일
Hi. I have a uitable that I populate with a variety of functions, each one has a different number of columns and colnames. I want to have a generic save function that will save regardless of the columns, and one where I can add the column names as a variable. This is my fixed column name approach. How do i adapt this for variable columns and column names.
%Save UI Table
tabledata = num2str(get(handles.uitable1,'data'));
fpath=get(handles.edit1,'String'); %get filepath from edit box
fname=get(handles.textFile,'String'); %get file name
ffull=[fpath,fname];
filename = 'C:\Output.txt';
fid = fopen(filename, 'w');
fprintf(fid, '%s\r\n', ffull);
rows=length(tabledata);
fprintf(fid,' %s %s %s %s %s %s %s\t\r\n', 'mean','mode','max','min','sd','min20','Dyn Range') %this part I want to be variable!!!!
for i = 1:rows
%tabledata(i,:)
% fprintf(fid, '%s\t %s\t %s\t %s\t %s\t %s\t %s\t \n', tabledata(i,:));
fprintf(fid, '%s\r\n', tabledata(i,:));
% fprintf(fid, '\r\n')
end
fclose(fid);
I also need to add that my table consists ofa mixture of strings and numbers by using a cell array.

채택된 답변

Jan
Jan 2016년 9월 26일
You can use one of the many tools to export a cell to a text file from the file exchange:
  댓글 수: 2
Jason
Jason 2016년 9월 26일
I need to include the headers too
Jan
Jan 2016년 9월 28일
@Jason: Writing the headers to a text file is easy. Afterwards you can use one of the tools from the FEX to append the data.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jason
Jason 2016년 9월 26일
편집: Jason 2016년 9월 26일
Ok, I have made some progress: Im still not able to assign the colnames properly though the the T??
tabledata = (get(handles.uitable1,'data'));
colnames=get(handles.uitable1,'columnname')
T = cell2table(tabledata,'VariableNames',colnames);
writetable(T,'output.dat')
I get the following:
colnames =
'File' 'mean(FG)' 'max(FG)' 'mean(BG)' 'Objects'
Error using cell2table (line 69)
'mean(FG)' is not a valid variable name.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by