Display header along with data in excel using GUI

2 次查看(过去 30 天)
A = [12.7 5.02 -98 63.9 0 -.2 56];
B = [2 3 4 5 6 7 8];
c= transpose([A;B]);
[file,path] = uiputfile('*.xlsx');
filename = fullfile(path,file);
xlswrite(filename,c);
Using above code, I can only save data in excel but not their corresponding headers A and B. How can I do it in GUI?

采纳的回答

Image Analyst
Image Analyst 2022-6-27
Try to make a table and then write it out with writetable
% Create sample data.
A = [12.7 5.02 -98 63.9 0 -.2 56];
B = [2 3 4 5 6 7 8];
% Make a table
t = table(A(:), B(:), 'VariableNames', {'A', 'B'})
% Create an output filename.
[baseFileName, folder] = uiputfile('*.xlsx');
fullFileName = fullfile(folder, baseFileName);
fprintf('Saving Excel file : "%s".\n', fullFileName)
% Write the workbook to disk.
writetable(t, fullFileName);
% Open it up to verify its contents.
if ispc
winopen(fullFileName);
end

更多回答(1 个)

Voss
Voss 2022-6-26
A = [12.7 5.02 -98 63.9 0 -.2 56];
B = [2 3 4 5 6 7 8];
% c= transpose([A;B]);
c = num2cell(transpose([A;B])); % make c a cell array
headers = {'A' 'B'}; % use whatever headers you want
[file,path] = uiputfile('*.xlsx');
filename = fullfile(path,file);
% xlswrite(filename,c);
xlswrite(filename,[headers; c]); % include the headers when writing

标签

Community Treasure Hunt

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

Start Hunting!

Translated by