exporting listbox to excel while adding spaces in between variable names

조회 수: 2 (최근 30일)
Good Morning All,
I have created a GUI which uses a listbox of some parameter names. I am looking to export the parameters chosen as column heads of an excel spreadsheet, however I want to know how to add a blank column in between each selected parameter.
I know how to obtain the Names and Index of the listbox by:
Variable_Name = get(handles.Parameter_Listbox,'String');
Index = get(handles.Parameter_Listbox,'Value');
Now to create a space between the names I thought I could use a loop to build a matrix and tried the following:
for i=1:length(Index)
OutputTitles(i)=[Variable_Name(Index), ' '];
end
Then I could just use xlswrite(Output_File,OutputTitles,Sheet1,Range1);
I was also wondering if anyone could tell me how to skip more than one column header too. Say I wanted to put the Parameter Name every 5th column instead.
Thanks so much,
Mel

채택된 답변

Aniruddha Katre
Aniruddha Katre 2014년 8월 19일
Your idea of using a loop to insert spaces should work. Note that "Variable_Name" will contain a cell array. So to insert a blank column between the variable names, you can execute a loop that looks like this:
spacing = {''};
OutputTitles = [];
for ii = 1:length(Index)
if isempty(OutputTitles)
OutputTitles = [OutputTitles Variable_Name(Index(ii))];
else
OutputTitles = [OutputTitles spacing Variable_Name(Index(ii))];
end
end
Then you can just call "xlswrite". If you want to put more empty columns between variable names, you can just add the number of empty cells in "spacing" as follows:
spacing = [{''},{''},{''}];
  댓글 수: 1
Melissa
Melissa 2014년 8월 19일
Thank you so much for your detailed explanation it really helped me and your code was spot on!

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

추가 답변 (1개)

Iain
Iain 2014년 8월 19일
Its playing with cell arrays:
Variable_Name = {'One','Two','Many'};
chosen_variable_names = Variable_Name(index);
Output_Table{1,1} = chosen_variable_names{1}; % column 1
Output_Table{1,3} = chosen_variable_names{2}; % column 3
Output_Table{1,45} = chosen_variable_names{3}; % column 45

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by