Impossible to set VariableTypes as 'double' for complete array and readtable function

조회 수: 12 (최근 30일)
Hi everyone,
I'm having trouble to set the VariableTypes for the 'opts' of the readtable function.
Either I can convert only the first column properly, either I get an error. Please see the different try in the code below (line 35 to 48).
I tried the solution of the different post, but it seem that I can't wrap my head around it.
Log file example : "test_log_1.csv"
Matlab code : Execute the function below with [data, xl]= AA_getSamplerData_Mat("test_log_1.csv");
function [data, xlHeader] = AA_getSamplerData_Mat(filename)
%Constant
EXCPECTED_PARAM = 26;
% Check if file exist
%todo
%% import and convert data ONLY FOR HEADER
opts = detectImportOptions(filename);
opts.Delimiter = ' ';
opts.DataLines = [2 EXCPECTED_PARAM+2];
opts.VariableNamingRule = 'preserve';
xlHeader = readtable(filename,opts);
xlHeader = table2array(xlHeader);
%Remove last column, empty for some reason
xlHeader (:,end) = [];
%% import and convert data ONLY FOR DATA
%Check if sampler header as been updated since last script writting
line=1;
while strfind(xlHeader{line,1},'#') == 1
line = line + 1;
end
if (EXCPECTED_PARAM + 1) ~= line
warning("Parameter of the sample file was modified, please edit function");
end
%Read data
opts = detectImportOptions(filename);
opts.Delimiter = ' ';
%Try 1 (uncomment as needed)
% tempVar = repmat("double", 1, length(xlHeader(end,:)));
% opts.VariableTypes = tempVar; %give "Cell array of types must be a vector of length 1"
%Try 2 (uncomment as needed)
opts.VariableTypes = 'double';
T = preview(filename,opts);%Only convert the first column...
%Try 3 (uncomment as needed)
% opts = setvartype(opts, xlHeader(end,:), 'double'); % give "Unknown variable name: 'target-position-x'."
%Try 4 (uncomment as needed)
% opts = setvartype(opts, 'double');
% T = preview(filename,opts);%Same result as try 2
opts.CommentStyle = '#';
opts.DataLines = line + 2;
opts.VariableNamesLine = line + 1;
opts.VariableNamingRule = 'preserve';
xlData = readtable(filename,opts);
%Remove last column, empty for some reason
xlData(:,end) = [];
end
Thanks you in advance for any help
  댓글 수: 2
Alexander
Alexander 2024년 3월 25일
It is always helpful to supply a executable script which shows the error you are dealing with.

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

채택된 답변

Stephen23
Stephen23 2024년 3월 25일
편집: Stephen23 2024년 3월 25일
Try something simpler and let READTABLE do the work for you:
T = readtable('test_log_1.csv', 'CommentStyle','#', 'VariableNamingRule','preserve')
T = 1500x32 table
target-position-x target-position-y target-position-z target-position-b target-position-c current-pos-X current-pos-Y current-pos-Z current-pos-B current-pos-C pos-error-0 pos-error-1 pos-error-2 pos-error-3 pos-error-4 current-amp-X current-amp-Y current-amp-Z current-amp-B current-amp-C current-amp-S data-selector machining-finished rt-process-time rt-loop-time rt-current-time in-fault-X in-fault-Y in-fault-Z in-fault-B in-fault-C in-fault-S _________________ _________________ _________________ _________________ _________________ _____________ _____________ _____________ _____________ _____________ ___________ ___________ ___________ ___________ ___________ _____________ _____________ _____________ _____________ _____________ _____________ _____________ __________________ _______________ ____________ _______________ __________ __________ __________ __________ __________ __________ -13.5 14 0 0 0 -13.5 14 1e-06 3e-05 0 9e-06 -6e-06 -1e-06 -3e-05 0 -0.083281 0.28555 0.85817 -1.1016 -0.19102 0.83771 0 1 3944 1.0016e+06 4.2033e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 -1e-06 -2.6e-05 0 -6e-06 4e-06 1e-06 2.6e-05 0 -0.083888 0.27075 0.86965 -1.0051 -0.19747 0.82054 0 1 5463 1.0026e+06 4.2043e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 5e-06 1.5e-05 0 -1e-06 3e-06 -5e-06 -1.5e-05 0 -0.082538 0.28272 0.86876 -1.0785 -0.1962 0.80785 0 1 4352 9.9687e+05 4.2053e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 7e-06 -2.8e-05 0 -6e-06 9e-06 -7e-06 2.8e-05 0 -0.096783 0.29503 0.86353 -0.99862 -0.19728 0.81297 0 1 4333 9.9891e+05 4.2063e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 6e-06 5e-06 0 -3.1e-05 2.4e-05 -6e-06 -5e-06 0 -0.089356 0.29515 0.86354 -1.0787 -0.20627 0.81926 0 1 4555 1.0001e+06 4.2073e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 -2e-06 -1.7e-05 0 -5.2e-05 4.7e-05 2e-06 1.7e-05 0 -0.092496 0.2997 0.85906 -1.0077 -0.19346 0.82981 0 1 3963 9.9952e+05 4.2083e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 -3e-06 1e-06 0 -5.1e-05 3.9e-05 3e-06 -1e-06 0 -0.09604 0.29417 0.86586 -1.0764 -0.20099 0.84651 0 1 4019 1.0009e+06 4.2093e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 -7e-06 -5e-06 0 -5.4e-05 4.1e-05 7e-06 5e-06 0 -0.099888 0.29184 0.86211 -1.0326 -0.19928 0.86566 0 1 3945 9.985e+05 4.2103e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 -1e-06 -2e-06 0 -4.7e-05 2.9e-05 1e-06 2e-06 0 -0.10313 0.28543 0.86895 -1.0834 -0.19601 0.88833 0 1 3981 1.0001e+06 4.2113e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 -1.6e-05 1.9e-05 0 -4.1e-05 3.1e-05 1.6e-05 -1.9e-05 0 -0.10063 0.30733 0.86288 -1.0587 -0.19276 0.9185 0 1 3926 9.9956e+05 4.2123e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 6e-06 1e-06 0 -4.7e-05 3.8e-05 -6e-06 -1e-06 0 -0.10208 0.30467 0.86363 -1.0754 -0.19622 0.94515 0 1 4315 1.0003e+06 4.2133e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 -1.6e-05 2.5e-05 0 -4.9e-05 3e-05 1.6e-05 -2.5e-05 0 -0.1059 0.28859 0.85929 -1.0655 -0.19409 0.94878 0 1 3870 9.9967e+05 4.2143e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 1.3e-05 -2e-05 0 -6e-05 3.2e-05 -1.3e-05 2e-05 0 -0.11359 0.31053 0.86301 -1.0464 -0.19245 0.97241 0 1 4315 1.0009e+06 4.2153e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 -1e-06 2.3e-05 0 -4e-05 2.4e-05 1e-06 -2.3e-05 0 -0.11103 0.30302 0.87176 -1.0671 -0.19506 0.9725 0 1 4148 9.9956e+05 4.2163e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 1.6e-05 -3.5e-05 0 -4.4e-05 3.4e-05 -1.6e-05 3.5e-05 0 -0.11548 0.31041 0.86156 -1.0192 -0.19022 0.98795 0 1 5093 1.0031e+06 4.2173e+09 0 0 0 0 0 0 -13.5 14 0 0 0 -13.5 14 7e-06 2.7e-05 0 -3.2e-05 2e-05 -7e-06 -2.7e-05 0 -0.11339 0.30971 0.86671 -1.0765 -0.19342 0.97006 0 1 4185 9.9672e+05 4.2183e+09 0 0 0 0 0 0
And for reading the header numeric parameters:
H = readtable('test_log_1.csv', 'Delimiter',':', 'MissingRule','omitrow',...
'ExpectedNumVariables',2, 'NumHeaderLines',3)
H = 24x2 table
Var1 Var2 ____________________________ ____ {'# NHorz' } 5 {'# NDiscr' } 20 {'# NBreak' } 10 {'# LSplit [mm]' } 3 {'# CutOff [mm]' } 0.1 {'# SplitSpecialSpline [-]'} 1 {'# vmax X [mm/s]' } 500 {'# vmax Y [mm/s]' } 500 {'# vmax Z [mm/s]' } 500 {'# vmax A [mm/s]' } 0 {'# vmax B [mm/s]' } 40 {'# vmax C [mm/s]' } 50 {'# amax X [mm/s^2]' } 270 {'# amax Y [mm/s^2]' } 270 {'# amax Z [mm/s^2]' } 270 {'# amax A [mm/s^2]' } 0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by