Writing to Excel

1 visualizzazione (ultimi 30 giorni)
Mel
Mel il 17 Mag 2011
Risposto: James Kristoff il 22 Mag 2014
I want to write a series of values to an excel file. The following is the code I am currently using. It works but is by no means efficient. Is there any way for me to write this more efficiently? if handles.current_data==handles.anterior
xlswrite(strfil, evalin('base','cursor_info(1,8).Position'),'Anterior','B2'); xlswrite(strfil, evalin('base','cursor_info(1,8).DataIndex'),'Anterior','D2'); xlswrite(strfil, evalin('base','cursor_info(1,7).Position'),'Anterior','E2'); xlswrite(strfil, evalin('base','cursor_info(1,7).DataIndex'),'Anterior','G2'); xlswrite(strfil, evalin('base','cursor_info(1,6).Position'),'Anterior','B3'); xlswrite(strfil, evalin('base','cursor_info(1,6).DataIndex'),'Anterior','D3'); xlswrite(strfil, evalin('base','cursor_info(1,5).Position'),'Anterior','E3'); xlswrite(strfil, evalin('base','cursor_info(1,5).DataIndex'),'Anterior','G3'); xlswrite(strfil, evalin('base','cursor_info(1,4).Position'),'Anterior','B4'); xlswrite(strfil, evalin('base','cursor_info(1,4).DataIndex'),'Anterior','D4'); xlswrite(strfil, evalin('base','cursor_info(1,3).Position'),'Anterior','E4'); xlswrite(strfil, evalin('base','cursor_info(1,3).DataIndex'),'Anterior','G4'); xlswrite(strfil, evalin('base','cursor_info(1,2).Position'),'Anterior','B5'); xlswrite(strfil, evalin('base','cursor_info(1,2).DataIndex'),'Anterior','D5'); xlswrite(strfil, evalin('base','cursor_info(1,1).Position'),'Anterior','E5'); xlswrite(strfil, evalin('base','cursor_info(1,1).DataIndex'),'Anterior','G5');
  1 Commento
Andy
Andy il 17 Mag 2011
Is there a reason you are evaluating your data in the base workspace? It seems you have not fully described your particular needs.

Accedi per commentare.

Risposte (1)

James Kristoff
James Kristoff il 22 Mag 2014
% first if you are trying to access data in your base workspace from within
% a function you can access it once and store it to a local variable
% instead of accessing the base workspace over and over.
cursor_info_local = evalin('base','cursor_info(1,:)');
% next, I noticed that the data is not well organized for inserting into an
% excel sheet, like you want to do. So we can fix that by fliping the
% data, so that it is in the same order as the rows we want to fill in the
% excel sheet.
cursor_info_local = fliplr(cursor_info_local(1,:));
% next we can create an array of all the data in each field of the
% structure
position = [cursor_info_local(:).Position];
dataIndex = [cursor_info_local(:).DataIndex];
%next we split the data into individual rows
% get the odd rows
position_row(1, :) = position(mod(1:length(position), 2) == 1);
dataIndex_row(1, :) = position(mod(1:length(dataIndex), 2) == 1);
% get the even rows
position_row(2, :) = position(mod(1:length(position), 2) == 0);
dataIndex_row(2, :) = position(mod(1:length(dataIndex), 2) == 0);
% now we can fill in the entire sheet:
% write the Position data to the excel sheet
xlswrite(strfil, position_row(1, :)', 'Anterior','B2');
xlswrite(strfil, position_row(2, :)', 'Anterior','E2');
% write the DataIndex data to the excel sheet
xlswrite(strfil, dataIndex_row(1, :)', 'Anterior','D2');
xlswrite(strfil, dataIndex_row(2, :)', 'Anterior','G2');

Community Treasure Hunt

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

Start Hunting!

Translated by