XLSWRITE alternative XLWRITE on a Mac
15 views (last 30 days)
Show older comments
Hi,
I'm running a custom program in Matlab that required xlswrite. However, since I'm working on a Mac, xlswrite is not available. I tried using xlwrite http://www.mathworks.com/matlabcentral/fileexchange/37560-xlwrite---export-data-to-excel-from-matlab-on-mac-win, but it's not really working, either.
My program requires to read the cells in an existing xls-file or create a new one and write to this. However, for some reason that doesn't seem to work.
Here is the code I need to get working
%column headers for excel spreadsheet
colhds_excel={'Input file', 'Source', 'Call type', ...
'Start time', 'Start Filename', 'End time', 'End Filename', 'Frequency 1','Frequency 2','Frequency 3','Frequency 4',...
'Comments', 'jpeg file?', '(e)wav file?'};
%if no logfile yet, ask them to specify one
if isempty(handles.logfilename) | isequal(handles.logfilename,0);
[handles.logfilename, handles.logfilepath]=uigetfile('*.xls', ...
'select a spreadsheet (hit ''cancel'' to create new sheet)');
%if user selects an existing spreadsheet:
if exist([handles.logfilepath '\' handles.logfilename])==2;
[xlnum, xltext, xlcell]= ...
xlsread([handles.logfilepath '\' handles.logfilename],1);
if isnumeric(xlcell)
if isnan(xlcell); %worksheet 1 has not been written to yet
xlwrite([handles.logfilepath '\' handles.logfilename],...
colhds_excel, 'Sheet1', 'A1'); %write col hdrs
lastrow=1;
end
else lastrow=size(xlcell,1);
end
rowstartnum=lastrow+1;
% T= xlcell(lastrow, 2); %read last event number
% t=char(T); %convert from cell to string
% lastevent=str2num(t(end-4:end)); %convert to number
% eventcount=lastevent+1; %add one to last event number
%if user selects cancel, allow user to create new file
elseif isequal(handles.logfilename,0) | isequal(handles.logfilepath,0);
[handles.logfilename,handles.logfilepath, filterindex] = ...
uiputfile('.xls', 'create new file');
%if cancel pushed again, get out of if loop
if isequal(handles.logfilename,0) | isequal(handles.logfilepath,0);
return
else xlwrite([handles.logfilepath '\' handles.logfilename],...
colhds_excel, 'Sheet1', 'A1'); %write col hdrs
% eventcount=1; %start event number at 1 for new spreadsheet
rowstartnum=2; %start writing data in 2nd row (1st row=headers)
end
end
%if logfile already exists, append to it starting
%at last row + 1; also need to add 1 to last event number
elseif exist([handles.logfilepath '\' handles.logfilename])==2;
[xlnum, xltext, xlcell]=xlsread([handles.logfilepath '\' handles.logfilename],1);
if isnumeric(xlcell)
if isnan(xlcell); %worksheet 1 has not been written to yet
xlwrite([handles.logfilepath '\' handles.logfilename],...
colhds_excel, 'Sheet1', 'A1'); %write col hdrs
lastrow=1;
end
else lastrow=size(xlcell,1);
end
rowstartnum=lastrow+1;
% T= xlcell(lastrow, 2); %read last event number
% t=char(T); %convert from cell to string
% lastevent=str2num(t(end-4:end)); %convert to number
% eventcount=lastevent+1; %add one to last event number
% set(handles.eventnumber,'String',...
% [get(handles.username, 'string')...
% handles.dateid '_' sprintf('%05d',handles.eventcount)]);
end
% handles.eventcount=eventcount;
% set(handles.eventnumber,'String',...
% [get(handles.username, 'string')...
% handles.dateid '_' sprintf('%05d',handles.eventcount)]);
% eventnum=get(handles.eventnumber, 'string');
%make row vector of data
%have to convert strings from char to cell
M=[{handles.infilename} handles.selectedspecies {calltype} ...
{startpick} {char(handles.startfilepick)} {endpick} {char(handles.endfilepick)} {frequency1} {frequency2} {frequency3} {frequency4}...
{comments} {jpegfilename} {wavefilename}];
%write to excel file
% outfname= {fullfile(handles.logfilepath, handles.logfilename)};
xlwrite([handles.logfilepath, '\', handles.logfilename], M, 'Sheet1', ['A' num2str(rowstartnum)]);
Amongst others, I do get the error that rowstartnum is undefined.
Any help is VERY much appreciated.
(MATLAB 2015b, MS Excel 2011 installed)
EDIT:
Ok, I think the issue is, that my program can't READ the existing file to begin with (of course it can't read the size, then, if it can't even open the file). Any work around for Mac?
0 Comments
Answers (2)
Walter Roberson
on 23 Nov 2015
Limitations
If your computer does not have Excel for Windows®, or if the COM server (part of the typical installation of Excel) is unavailable, then the xlswrite function:
- Writes array A to a text file in comma-separated value (CSV) format. A must be a numeric matrix.
- Ignores the sheet and xlRange arguments.
1 Comment
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!