XLSWRITE alternative XLWRITE on a Mac

조회 수: 15 (최근 30일)
Anke Kügler
Anke Kügler 2015년 11월 22일
편집: Anke Kügler 2015년 11월 23일
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?

답변 (2개)

John D'Errico
John D'Errico 2015년 11월 23일
Gosh, I wonder why xlswrite is in my release, ON MY MAC.
  댓글 수: 1
Anke Kügler
Anke Kügler 2015년 11월 23일
Is it? I was trying my code with xlswrite, but it was not working. It was writing csv-files (and than f** up when trying to read from the non-existing xls-file), as expected when reading older comments on the issue.
The xlsread-issue remains, too.

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


Walter Roberson
Walter Roberson 2015년 11월 23일
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
Anke Kügler
Anke Kügler 2015년 11월 23일
편집: Anke Kügler 2015년 11월 23일
Ok, I downloaded this version and installed the POI library, but it's still not working. When trying with an example, I get the following error:
Error using xlwrite (line 286)
Java exception occurred:
java.io.FileNotFoundException: myExample.xls (Permission denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
What now?
Edit:
Nevermind, I wasn't paying attention to my paths and was trying to write to one where I don't have access to. Seems to be working, now.
However, the xslread remains a major issue. I already included the 'basic' part in the arguments and it seems to be reading my test file. However, I still don't get the rowstartnum. Does anybody have any idea? Thank you very much!

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

카테고리

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