xlsxwrite is not working. Do I need MS Excel pre-installed to use xlswrite

When I run the following code, I get the error "Existing file C:/Data.......(hereis the folder path) may be open. Please close the file and try again". I have pasted only the last part of the code.
Could someone please suggest how to get the three lines working.
xlsxwrite([FilePath FileName],[PeakLabels;num2cell(PeakData)],1)
xlsxwrite([FilePath FileName],[SeriesLabels;num2cell(SeriesData)],2)
xlsxwrite([FilePath FileName],[SummaryLabels;num2cell(SummaryData)],3)
I do not really need the output as MS Excel file, any other table format including csv or txt is also fine. Do I need MS Excel pre-installed to use xlswrite? I do not have MS Excel installed.
Any feedback will be of great help.
Many thanks!
Here is the code:
% A third sheet brings it ALL together....
SummaryData = zeros(1,8);
SummaryLabels ={'Mean Number of Signal Events', 'Standard Deviation of the Number of Signal Events', 'Mean Height (all)', 'Mean Rise Time (all)', 'Mean Decay Time (all)', 'Mean FWHM (all)', 'Mean InterEvent Interval (all)', 'Mean Frequency (all)'};
SummaryData(1) = mean(SeriesData(:,2));
SummaryData(2) = std(SeriesData(:,2));
SummaryData(3) = SeriesData(:,2)'*SeriesData(:,3)/sum(SeriesData(:,2));
SummaryData(4) = SeriesData(:,2)'*SeriesData(:,4)/sum(SeriesData(:,2));
SummaryData(5) = SeriesData(:,2)'*SeriesData(:,5)/sum(SeriesData(:,2));
SummaryData(6) = SeriesData(:,2)'*SeriesData(:,6)/sum(SeriesData(:,2));
SummaryData(7) = max(SeriesData(:,2)'-1,0)*SeriesData(:,7)/sum(max(SeriesData(:,2)'-1,0));
SummaryData(8) = 1/SummaryData(7);
% Delete the file if it already exists....
if exist([FilePath FileName],'file')
delete([FilePath FileName])
end
% % And write a new file!
try
xlsxwrite([FilePath FileName],[PeakLabels;num2cell(PeakData)],1)
xlsxwrite([FilePath FileName],[SeriesLabels;num2cell(SeriesData)],2)
xlsxwrite([FilePath FileName],[SummaryLabels;num2cell(SummaryData)],3)
catch
fprintf(1, 'Error.\n');
warndlg(['Existing file ' [FilePath FileName] ' may be open. Please close the file and try again.'], 'Predictors
Not Saved')
return
end
end

4 Kommentare

Sorry, I forgot to include this part of the code in the begininning. This segment of the code specifies the location and file type.
function save_output_callback(~,~,h)
% The data is saved to three sheets of a spreadsheet. The user can choose
% the name of the spreadsheet. For simplicity, a name related to the
% original data file is suggested as a default....
SuggestedFileName = strsplit(get(h.file_textbox, 'String'),'.');
SuggestedFileName = SuggestedFileName{1};
[FileName, FilePath] = uiputfile(...
{'*.xls';'*.*'},...
'Save or append to file....',...
[SuggestedFileName '_peaks']);
if ~FileName
return
end
you should be using fullfile(FilePath,FileName) instead of [FilePath FileName] to construct the file name. Most of the routes to getting a file path do not promise that the character vector will end in an appropriate directory separator -- in particular, uiputfile() is inconsistent on that point.
You are using Windows. In Windows, the default is that a file that is open in one process becomes locked for reading and writing by other processes. Programs have to specifically tell Windows that it is okay to share the file for reading or writing. Excel does not do that -- so if a file is open in Excel, then it is not accessible to other processes, even if Excel is just holding it open for days on end "for efficiency".
Many thanks for the comment.
Infact, I do not have MS Excel installed at all. I am wondering is it necessary to have MS EXcel installed to efficiently use "xlsxwrite"? If it is the fact that MS Excel is not installed on this computer is the cause for the error, I can purchase and install MS Office on the computer.
Since I am not a programmer, I might not be able to modify the code extesnsively -installing MS Excel will be the easier option for me if this is the root cause of the problem.
I am using Windows 10, Matlab 2022a, Matlab version 9.12.
Thank you again.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 24 Dez. 2022
Use writetable() instead of xlswrite() or writematrix()
For .xls or .xlsx or .csv, writetable and writematrix() work perfectly well. De facto, to be able write and read data from such files (xls, xlsx, csv), you'd need to have MS Excel installed in your computer.

4 Kommentare

Stephen23
Stephen23 am 24 Dez. 2022
Bearbeitet: Stephen23 am 24 Dez. 2022
"De facto, to be able write and read data from such files (xls, xlsx, csv), you'd need to have MS Excel installed in your computer."
CSV files are text files which exist completely independently from MS Excel. MS Excel has never been required, is not required, and never will be required for reading or writing (CSV) text files. Using Excel to modify or look at the content of CSV files is the cause of many problems, because it changes data without warning:
For XLS/XLSX that depends on the function used:
  • the XLSWRITE() documentation states "If your computer does not have Excel for Windows® or you are using MATLAB® Online™, then the xlswrite function ... writes array A to a text file in comma-separated value (CSV) format. A must be a numeric matrix..."
  • the WRITECELL/WRITEMATRIX/WRITETABLE documentation states no such restriction for XLS/XLSX files. The only restriction given is for XLSB files, which require Excel to be installed on Windows.
I totally agree with Stephen's comments. I incorrectly included .csv along with .xls and .xlsx file types for MS Excel. Using writetable and writematrix() are recommended functions by MATLAB. xlswrite() becomes absolete in the future versions of MATLAB: https://www.mathworks.com/help/matlab/ref/xlswrite.html
Thank you for the reply. I changed xlswrite to writetable as shown below, but still have the error message. I am not sure if the syntax I used is correct.
Any advise on how to solve this will be very helpful. Many thanks!
writetable([FilePath FileName],[PeakLabels;num2cell(PeakData)],1)
writetable([FilePath FileName],[SeriesLabels;num2cell(SeriesData)],2)
writetable([FilePath FileName],[SummaryLabels;num2cell(SummaryData)],3)
% xlsxwrite([FilePath FileName],[PeakLabels;num2cell(PeakData)],1)
% xlsxwrite([FilePath FileName],[SeriesLabels;num2cell(SeriesData)],2)
% xlsxwrite([FilePath FileName],[SummaryLabels;num2cell(SummaryData)],3)
The full segment of the function is below.
%%
function save_output_callback(~,~,h)
% The data is saved to three sheets of a spreadsheet. The user can choose
% the name of the spreadsheet. For simplicity, a name related to the
% original data file is suggested as a default....
SuggestedFileName = strsplit(get(h.file_textbox, 'String'),'.');
SuggestedFileName = SuggestedFileName{1};
[FileName, FilePath] = uiputfile(...
{'*.csv';'*.txt';'*.dat'},...
'Save or append to file....',...
[SuggestedFileName '_peaks']);
if ~FileName
return
end
% Go get all of the data that we wish to summarize and save....
if get(h.shutter_checkbox,'Value')
Observations = get(h.shutter_free_table, 'Data');
else
Observations = get(h.observation_table, 'Data');
end
SmoothedObservations = get(h.observation_table, 'UserData');
Peaks = get(h.peak_table, 'Data');
Time = get(h.time_table, 'Data');
HalfBefore = get(h.half_table, 'Data');
HalfAfter = get(h.half_table, 'UserData');
[T,N] = size(Peaks);
% If there's no time series provided, we improvise....
if length(Time) ~= T
Time = 1:T;
end
% Pull out the peaks and the valleys....
[PeakLocation,PeakIndex] = find(Peaks);
[HalfBeforeLocation,~] = find(HalfBefore);
[HalfAfterLocation,~] = find(HalfAfter);
NumPeaks = length(PeakLocation);
% Now we put together the first sheet of data about the individual
% peaks....
PeakData = zeros(NumPeaks,7);
PeakLabels ={'Series', 'Peak Number', 'Time', 'Height', 'Rise Time (half max to max)', 'Decay Time (max to half max)', 'FWHM'};
BigCount = 1;
for ii=1:N
for jj = 1:sum(PeakIndex==ii)
PeakData(BigCount,1) = ii;
PeakData(BigCount,2) = jj;
PeakData(BigCount,3) = Time(PeakLocation(BigCount));
PeakData(BigCount,4) = Observations(PeakLocation(BigCount),PeakIndex(BigCount))./SmoothedObservations(PeakLocation(BigCount),PeakIndex(BigCount)) - 1;
PeakData(BigCount,5) = Time(PeakLocation(BigCount))-Time(HalfBeforeLocation(BigCount));
PeakData(BigCount,6) = Time(HalfAfterLocation(BigCount))-Time(PeakLocation(BigCount));
PeakData(BigCount,7) = Time(HalfAfterLocation(BigCount))-Time(HalfBeforeLocation(BigCount));
BigCount = BigCount+1;
end
end
% A second sheet collects data about the series (or ROIs)....
SeriesData = zeros(N,8);
SeriesLabels ={'Series', 'Number of Events', 'Mean Height', 'Mean Rise Time', 'Mean Decay Time', 'Mean FWHM', 'Mean InterEvent Interval', 'Mean Frequency'};
for ii = 1:N
SeriesData(ii,1) = ii;
Indices = find(PeakIndex==ii);
SeriesData(ii,2) = length(Indices);
if ~isempty(Indices)
SeriesData(ii,3) = mean(PeakData(Indices,4));
SeriesData(ii,4) = mean(PeakData(Indices,5));
SeriesData(ii,5) = mean(PeakData(Indices,6));
SeriesData(ii,6) = mean(PeakData(Indices,7));
end
if length(Indices) > 1
SeriesData(ii,7) = mean(PeakData(Indices(2:end),3)-PeakData(Indices(1:end-1),3));
SeriesData(ii,8) = 1/mean(PeakData(Indices(2:end),3)-PeakData(Indices(1:end-1),3));
end
end
% A third sheet brings it ALL together....
SummaryData = zeros(1,8);
SummaryLabels ={'Mean Number of Signal Events', 'Standard Deviation of the Number of Signal Events', 'Mean Height (all)', 'Mean Rise Time (all)', 'Mean Decay Time (all)', 'Mean FWHM (all)', 'Mean InterEvent Interval (all)', 'Mean Frequency (all)'};
SummaryData(1) = mean(SeriesData(:,2));
SummaryData(2) = std(SeriesData(:,2));
SummaryData(3) = SeriesData(:,2)'*SeriesData(:,3)/sum(SeriesData(:,2));
SummaryData(4) = SeriesData(:,2)'*SeriesData(:,4)/sum(SeriesData(:,2));
SummaryData(5) = SeriesData(:,2)'*SeriesData(:,5)/sum(SeriesData(:,2));
SummaryData(6) = SeriesData(:,2)'*SeriesData(:,6)/sum(SeriesData(:,2));
SummaryData(7) = max(SeriesData(:,2)'-1,0)*SeriesData(:,7)/sum(max(SeriesData(:,2)'-1,0));
SummaryData(8) = 1/SummaryData(7);
% Now delete the file if it already exists....
if exist([FilePath FileName],'file')
delete([FilePath FileName])
end
% % And write a new one!!
try
writetable([FilePath FileName],[PeakLabels;num2cell(PeakData)],1)
writetable([FilePath FileName],[SeriesLabels;num2cell(SeriesData)],2)
writetable([FilePath FileName],[SummaryLabels;num2cell(SummaryData)],3)
% xlsxwrite([FilePath FileName],[PeakLabels;num2cell(PeakData)],1)
% xlsxwrite([FilePath FileName],[SeriesLabels;num2cell(SeriesData)],2)
% xlsxwrite([FilePath FileName],[SummaryLabels;num2cell(SummaryData)],3)
catch
fprintf(1, 'Error.\n');
warndlg(['Existing file ' [FilePath FileName] ' may be open. Please close the file and try again.'], 'Predictors Not Saved')
return
end
end
Stephen23
Stephen23 am 27 Dez. 2022
Bearbeitet: Stephen23 am 27 Dez. 2022
The WRITETABLE documentation clearly shows that its first input must be a table, and its second input is the filename. You have swapped them around, which will not work.
Your code does not seem to use tables, so WRITETABLE is unlikely to be useful for you anyway. Most likely you need WRITEMATRIX or WRITECELL.
Also replace [FilePath FileName] with FULLFILE(..)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

I could get the values that I am interested in by using:
C = ([SeriesLabels;num2cell(SeriesData)]);
writecell(C,'newtrialjck.csv')
or by using
P = [SeriesLabels;num2cell(SeriesData)];
save P.mat
Thank you for your suggestions.

Produkte

Version

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by