MATLAB Answers

How can I add path in Matlab to read files csv and excel files from specific folder and read the data?

25 views (last 30 days)
Arshey Dhangekar
Arshey Dhangekar on 16 Jun 2021
Edited: Stephen on 20 Jun 2021
I want to add path in my script so that it can read and store csv and excel files in struct. I want to add "D:\AMXE132#03\200917\Duty-01\BL" or any path in script to access csv and excel files.
%my path D:\AMXE132#03\200917\Duty-01\BL
Also
I tried import data from csv file but it is detecting var 1 var 2.. Var 3 as header. Row 38 header is getting. How can solve the header reading issue
%Import the options of the csv file
opts=detectImportOptions('WT_ - 10-15.csv');
%Defines the row location of channel variable name
opts.VariableNamesLine = 38;
%Specifies that the data is comma seperated
opts.Delimiter =','; %Specifies that the data is comma seperated
%Read the table
t = readtable('WT_ - 10-15.csv',opts, 'ReadVariableNames', true);

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jun 2021
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/655500/WT_%20-%2010-15.csv';
t = readtable(filename, 'HeaderLines', 37, 'VariableNamingRule', 'preserve');
Warning: Table variable names were truncated to the length namelengthmax. The original names are saved in the VariableDescriptions property.
t.Properties.VariableNames(1:2)
ans = 1×2 cell array
{'Store No.;Date;Time;Millisecond;U-SigmaA-Total-WT1;I-SigmaA-Tot'} {'Var2'}
Your variable names line uses semi-colon as the delimiter, not comman.
vnc = readcell(filename, 'Range', '38:38');
vn = matlab.lang.makeUniqueStrings(regexp(vnc{1}, ';', 'split'))
vn = 1×52 cell array
{'Store No.'} {'Date'} {'Time'} {'Millisecond'} {'U-SigmaA-Total-WT1'} {'I-SigmaA-Total-WT1'} {'P-SigmaA-Total-WT1'} {'P-SigmaA-1-WT1'} {'PF-1-1-WT1'} {'FreqU-1-Total-WT1'} {'U-SigmaA-1-WT1'} {'I-SigmaA-1-WT1'} {'Uthd-1-Total-WT1'} {'Ithd-1-Total-WT1'} {'Speed-1-Total-WT1'} {'Torque-1-Total-WT1'} {'Pm-1-Total-WT1'} {'Eta1-1-Total-WT1'} {'U-4-Total-WT1'} {'I-4-Total-WT1'} {'P-4-Total-WT1'} {'Eta2-1-Total-WT1'} {'U-1-Total-WT1'} {'U-2-Total-WT1'} {'U-3-Total-WT1'} {'I-1-Total-WT1'} {'I-2-Total-WT1'} {'I-3-Total-WT1'} {'U-SigmaA-Total-WT2'} {'I-SigmaA-Total-WT2'} {'P-SigmaA-Total-WT2'} {'P-SigmaA-1-WT2'} {'PF-1-1-WT2'} {'FreqU-1-Total-WT2'} {'U-SigmaA-1-WT2'} {'I-SigmaA-1-WT2'} {'Uthd-1-Total-WT2'} {'Ithd-1-Total-WT2'} {'PF-1-Total-WT1'} {'PF-1-Total-WT2'} {'Pm-1-Total-WT1_1'} {'Eta1-1-Total-WT1_1'} {'U-4-Total-WT2'} {'I-4-Total-WT2'} {'P-4-Total-WT2'} {'Eta2-1-Total-WT2'} {'U-1-Total-WT2'} {'U-2-Total-WT2'} {'U-3-Total-WT2'} {'I-1-Total-WT2'} {'I-2-Total-WT2'} {'I-3-Total-WT2'}
t = readtable(filename, 'HeaderLines', 38, 'ReadVariableNames', false);
t.Properties.VariableNames = vn;
t(1,:)
ans = 1×52 table
Store No. Date Time Millisecond U-SigmaA-Total-WT1 I-SigmaA-Total-WT1 P-SigmaA-Total-WT1 P-SigmaA-1-WT1 PF-1-1-WT1 FreqU-1-Total-WT1 U-SigmaA-1-WT1 I-SigmaA-1-WT1 Uthd-1-Total-WT1 Ithd-1-Total-WT1 Speed-1-Total-WT1 Torque-1-Total-WT1 Pm-1-Total-WT1 Eta1-1-Total-WT1 U-4-Total-WT1 I-4-Total-WT1 P-4-Total-WT1 Eta2-1-Total-WT1 U-1-Total-WT1 U-2-Total-WT1 U-3-Total-WT1 I-1-Total-WT1 I-2-Total-WT1 I-3-Total-WT1 U-SigmaA-Total-WT2 I-SigmaA-Total-WT2 P-SigmaA-Total-WT2 P-SigmaA-1-WT2 PF-1-1-WT2 FreqU-1-Total-WT2 U-SigmaA-1-WT2 I-SigmaA-1-WT2 Uthd-1-Total-WT2 Ithd-1-Total-WT2 PF-1-Total-WT1 PF-1-Total-WT2 Pm-1-Total-WT1_1 Eta1-1-Total-WT1_1 U-4-Total-WT2 I-4-Total-WT2 P-4-Total-WT2 Eta2-1-Total-WT2 U-1-Total-WT2 U-2-Total-WT2 U-3-Total-WT2 I-1-Total-WT2 I-2-Total-WT2 I-3-Total-WT2 _________ ______________ ________ ___________ __________________ __________________ __________________ ______________ __________ _________________ ______________ ______________ ________________ ________________ _________________ __________________ ______________ ________________ _____________ _____________ _____________ ________________ _____________ _____________ _____________ _____________ _____________ _____________ __________________ __________________ __________________ ______________ __________ _________________ ______________ ______________ ________________ ________________ ______________ ______________ ________________ __________________ _____________ _____________ _____________ ________________ _____________ _____________ _____________ _____________ _____________ __________________________________________________________________ 2 {'2020/09/17'} 08:38:20 241 0.6394 0 -0.1 0 0.01121 NaN 0.0084 0.05 99.819 99.727 NaN 0.30269 NaN NaN 599.18 -0.02 -13 0.521 0.4926 0.641 0.7846 0 0 0 4.7373 0 0.2 0 -0.49986 149.94 0.0533 0.05 99.699 99.368 NaN NaN NaN NaN 598.98 0.03 18 NaN 4.7133 4.7517 4.747 0 0 {'0.00000E+03;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'}
Note that Pm-1-Total-WT1 occurs twice, so the second one had to be renamed to Pm-1-Total-WT1_1
  6 Comments
Walter Roberson
Walter Roberson on 19 Jun 2021
That did not answer the question.
Should the volunteers answer only exactly what was asked? Or should the volunteers try to show the person better ways of doing what the volunteers (with their experience) figure that the person actually needs ?

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!