Need help with import variables with readtable Function
显示 更早的评论
I am currently trying to create a code that imports 3 .csv files. The problem that i am having is that anything past the first table the code does not keep the variables and changes them to VarX. Has anyone experienced this before? How did you fix it other than having to rename the variables. The error that i get is Unrecognized variable name 'pt1_X'. Thank you in advanced.
%Set variables
u = symunit;
%Rod diameter
n=9.7 * u.mm;
% read table
%XYZ of Cockroach
[file, path] = uigetfile('*.csv');
tbl = readtable([path file]);
%XYZ of Rod
[file, path] = uigetfile('*.csv');
tbl2 = readtable([path file]);
%get rod points and remove NaN from table
tbl2([1:150],:) = [];
tbl2([11:end],:) = [];
%{
%XYZ of Gravity refernce
[file, path] = uigetfile('*.csv');
tbl3 = readtable([path file]);
%get gravity points and remove NaN from table
tbl3([1:150],:) = [];
tbl3([11:end],:) = [];
%}
% plot all points, blue is the head
figure
plot3(tbl.pt1_X, tbl.pt1_Y, tbl.pt1_Z, 'b.')
hold on
plot3(tbl.pt2_X, tbl.pt2_Y, tbl.pt2_Z, 'r.')
%Plot rod
plot3(tbl2.pt1_X, tbl2.pt1_Y, tbl2.pt1_Z, 'kx')
hold on
plot3(tbl2.pt2_X, tbl2.pt2_Y, tbl2.pt2_Z, 'kx')
hold on
plot3(tbl2.pt3_X, tbl2.pt3_Y, tbl2.pt3_Z, 'kx')
hold on
plot3(tbl2.pt4_X, tbl2.pt4_Y, tbl2.pt4_Z, 'kx')
hold on
plot3(tbl2.pt5_X, tbl2.pt5_Y, tbl2.pt5_Z, 'kx')
hold on
plot3(tbl2.pt6_X, tbl2.pt6_Y, tbl2.pt6_Z, 'kx')
hold on
3 个评论
TADA
2019-5-16
without a sample of the data (even mocked data) this is no more than guesswork, but
readtable changes the variable names to something that is acceptable by the parser
So any characters in the variable name other than alphanumeric and underscore are chnaged to something valid.
If you upload a sample of the data it would be a lot easier to help you stitch a solution
Thallon Pitchure
2019-5-16
Thallon Pitchure
2019-5-16
回答(1 个)
Jayanti
2025-2-14
Hi Thallon,
Sometimes, CSV files may contain hidden characters or formatting issues as a result MATLAB does not interpret it as a header. MATLAB provide “detectImportOptions” function to create an import options object for reading data from a file.
By using “detectImportOptions” with “NumHeaderLines”, 0, you can instruct MATLAB to treat the first line of the file as the header row. This is useful when you want to ensure that the column names are correctly interpreted.
Please refer to the below code for your reference:
opts = detectImportOptions(inputPath, 'NumHeaderLines', 0);
tbl = readtable(inputPath, opts);
I am also attaching documentation link on “detectImportOptions”for your reference:
类别
在 帮助中心 和 File Exchange 中查找有关 Tables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

