我想问一下如何使用我们从现有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中,然后我可以将rainfall.xls写入matlab中的表格。我想将它集成到我通过导入工具生成它的代码中:
[![%% 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;
你能帮帮我吗?非常感谢你的帮助。
答案 0 :(得分:1)
"dependencies": {
"System.Runtime.Serialization.Primitives": "4.0.10-*"
}
读取您最终重写的二进制文件(您看到的混乱),而不是使用fread
来读取表格文件。
答案 1 :(得分:0)
如果您所做的只是将1个文件复制到另一个文件而不更改任何内容,那么只需使用copyfile
即可。
copyfile(fullfile(filepath,filename),fullfile(filepath,'rainfall.xls'))
然后,您可以使用其他现有代码来读取和格式化数据。您所要做的就是将文件名换成fullfile(filepath,'rainfall.xls')