在matlab中从现有的xls文件中写入新的xls文件时出错

我想问一下如何用现有的xls文件确定的名称来编写新的xls文件。 我试过这个代码,它可以生成新的文件,但我不能打开它,因为它可能会损坏。 我必须使用uigetfile,因为它可以让另一个用户运行代码。 这是我的代码:

%% Select rainfall data % Call uigetfile by specifying file name and path as output [filename,filepath] = uigetfile('*.xls','Select the rainfall data'); % gets directory from any folder fido=fopen(fullfile(filepath,'rainfall.xls'),'w'); % open output file to write fidi=fopen(fullfile(filepath,filename)); % open input file fwrite(fido,fread(fidi,'*char')); % copy to output fclose(fidi); % close that input file fido=fclose(fido); clear fid* 

它可以生成rainfall.xls,但是当我通过excel打开文件时,内容乱七八糟(附图)。 我也附上现有的xls文件。 实际上我想将现有的文件复制到rainfall.xls中,然后我可以在matlab中将rainfall.xls保存到表中。 我想将它集成到通过导入工具生成的代码中:

 [![%% Import data from spreadsheet % Script for importing data from the following spreadsheet: % % Workbook: D:\TUM\Study Project\Study Project TUM\raw data\rain gauge\ULL00317_20171102.xls % Worksheet: Tabelle1 % % To extend the code for use with different selected data or a different % spreadsheet, generate a function instead of a script. % Auto-generated by MATLAB on 2017/11/03 19:36:55 %% Import the data, extracting spreadsheet dates in Excel serial date format \[~, ~, raw, dates\] = xlsread('D:\TUM\Study Project\Study Project TUM\raw data\rain gauge\ULL00317_20171102.xls','Tabelle1','A:F','',@convertSpreadsheetExcelDates); raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''}; raw = raw(:,\[2,3,4,5,6\]); dates = dates(:,1); %% Replace non-numeric cells with NaN R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells raw(R) = {NaN}; % Replace non-numeric cells R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),dates); % Find non-numeric cells dates(R) = {NaN}; % Replace non-numeric Excel dates with NaN %% Create output variable data = reshape(\[raw{:}\],size(raw)); %% Create table ULL0031720171102 = table; %% Allocate imported array to column variable names ULL0031720171102.DatumUhrzeit = datetime(\[dates{:,1}\].', 'ConvertFrom', 'Excel'); ULL0031720171102.ULL00317_3 = data(:,1); ULL0031720171102.ULL00317_CH31 = data(:,2); ULL0031720171102.ULL00317_CH32 = data(:,3); ULL0031720171102.ULL00317_1 = data(:,4); ULL0031720171102.ULL00317_2 = data(:,5); % For code requiring serial dates (datenum) instead of datetime, uncomment % the following line(s) below to return the imported dates as datenum(s). % ULL0031720171102.DatumUhrzeit=datenum(ULL0031720171102.DatumUhrzeit); %% Clear temporary variables clearvars data raw dates R; 

你可以帮我吗? 非常感谢您的帮助。

这是现存的xls文件: 现有的xls文件

这是一个混乱的新的xls文件: 新的xls文件

fread读取最终重写的二进制文件(您看到的混乱),而是使用xlsread来读取表格文件。

如果你正在做的是复制1文件到另一个没有改变任何东西,然后只使用copyfile

 copyfile(fullfile(filepath,filename),fullfile(filepath,'rainfall.xls')) 

然后,您可以使用其他现有的代码来读取和格式化他的数据。 你所要做的就是用fullfile(filepath,'rainfall.xls')交换文件名fullfile(filepath,'rainfall.xls')