Need help with import variables with readtable Function

22 views (last 30 days)
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 Comments

Sign in to comment.

Answers (1)

Jayanti
Jayanti on 14 Feb 2025 at 14:25
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:

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!