【问题标题】:MATLAB time duration in milliseconds from an excel file来自 excel 文件的 MATLAB 持续时间(以毫秒为单位)
【发布时间】:2021-07-01 15:03:07
【问题描述】:

我正在制作一个 Matlab 程序,该程序使用从打开文件对话框指定的 excel 文件中的数据。

[filename, pathname] = uigetfile({'*.xlsx','Excel Files(*.xlsx)'; '*.txt','Txt Files(*.txt)'}, 'Pick a file');
FilePath = append(pathname,filename);

opts = detectImportOptions(FilePath, "ReadVariableNames", false);
opts = setvartype(opts, 1, 'char');
data = readtable(FilePath, opts);

table = data(:,1);

现在代码是这样的。

enter image description here 之后,如您所见,日期保存为字符串。

但我真正想找到的是以毫秒为单位的时间差(持续时间)。

原始数据如下所示: enter image description here

A列和C列时间相同,所以我只想使用A列数据。

请帮助新手!我很感激!

【问题讨论】:

  • 尝试使用 Home / Import Data 工具先尝试手动导入,然后选择“创建函数”创建导入函数而不是导入数据。查看生成函数中的代码应该可以帮助您弄清楚...如果没有,请报告。

标签: matlab


【解决方案1】:

问题已解决。

我分享我的代码来帮助遇到同样问题的其他人。

如果事情可以更简化,请离开 cmets。

  1. 导入 Excel 文件进行分析
% Clean the memory and the code previously running
clc;
clear all;
close all;

% Sampling frequency of the acquired data
fs = 1e2; % Sampling Frequency - this can be found on LabView code.
Ts = 1/fs; % Sampling Interval

%Importing data from an excel file
[filename, pathname] = uigetfile({'*.xlsx','Excel Files(*.xlsx)'; '*.txt','Txt Files(*.txt)'}, 'Pick a file');
FilePath = append(pathname,filename);

[fPath ,fName, fExt] = fileparts(FilePath);

一个。从文件中查找“持续时间”

opts = spreadsheetImportOptions("NumVariables", 1);

% Specify sheet and range
opts.Sheet = "sheet1";
opts.DataRange = "A2";

% Specify column names and types
opts.VariableNames = "Time";
opts.VariableTypes = "datetime";

% Specify file level properties
opts.ImportErrorRule = "omitrow";
opts.MissingRule = "omitrow";

% Specify variable properties
opts = setvaropts(opts, "Time", "InputFormat", 'mm:ss.SSS');

tTable = readtable(FilePath, opts, "UseExcel", false);
tArray = table2array(tTable);

% Calculating time duration
tArray = tArray - tArray(1);

% Coverting to seconds
time = milliseconds(tArray)*1e-3;

% Clear temporary variables
clear opts;

% Discarding data if time difference is too big
ii = size(time(:,1));
k = 0;
disp('Now removing error data elements');

for i = 1:1:ii(1)-1
    a = time(i);
    b = time(i+1);
    if (b-a)>0.5 && k==0
        k=i+1;
        
        fprintf('Elements from %d seconds will be removed. (%dth element)\n', time(k),k);
        
        for j = ii(1):-1:k
            
            if rem(j,10)==0
                fprintf('%dth element is removed... \n',j);
            end
            
            time(j) = [];
        end
        
        break % Break the loop after removing error data
    end
end

disp('Time table is set.');

clearvars -except filename FilePath fs pathname time k ii Ts fName

【讨论】:

    【解决方案2】:

    我创建了一个类似的 excel 文件,我发现时间列被读取为一个字符。所以,我所做的是获取这些列的索引并将其转换为数据时间。之后,看起来它正在工作。希望这行得通。

    % Define the excel file name (can be the path)
    ExcelFile = 'demo_stackflow.xlsx';
    
    % Get the options for that sheet and preserving the variable name
    opts = detectImportOptions(ExcelFile,'Sheet','Sheet1',...
        "VariableNamingRule","preserve")
    % Get the idx where the variable is a char (In this case col 1 and 3)
    CharVars = opts.VariableNames(contains(opts.VariableTypes,'char'));
    % Convert the char columns to datetime
    opts = setvaropts(opts,CharVars,'Type',"datetime");
    % Get the data
    data = readtable(ExcelFile, opts)
    % Print the 1st column to see if the data type of the column is
    %  datetime
    T = data(:,1)
    

    【讨论】:

    • 这不起作用。但谢谢你的时间!使用 detectImportOptions 时出错(第 381 行)所有参数都必须有一个关联的值。
    • 你能分享错误信息吗?也许我在“示例”中假设了某种数据类型。
    • 我解决了这个问题。我将发布我的代码
    猜你喜欢
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多