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

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

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

답변 (1개)

Jayanti
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에 대해 자세히 알아보기

태그

질문:

2019년 5월 16일

답변:

2025년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by