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

조회 수: 37 (최근 30일)
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);

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 16일
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
Walter Roberson
Walter Roberson 2021년 6월 18일
So only asked how to add path of current folder
There is a long-standing disagreement about how volunteers should respond to questions here on MATLAB Answers. Perhaps you can resolve it for us:
When someone asks a question here, should the volunteers:
[A] Only answer exactly to what the person asks, and if the person obviously asks the wrong question, then answer to what they asked without discussing about what else they might mean. For example, if someone tries to use [] for indexing, instead of showing them how to use () or {} for indexing, tell them they need to open a technical support case to have Mathworks "fix" the "bug" in MATLAB, instead of telling them about () indexing; OR
[B] Answer to the absolute closest to what the person asked that can be achieved in MATLAB, even if it takes dozens of pages of code but what the person probably wanted might be solved in just a few characters. That is, should the volunteers assume that if someone asked to do something a particular way, that the person always has a Good Reason to want to do it that way, and it isn't the business of the volunteers to offer advice? Last week there was an instance in which to solve what the person actually asked would have required programming a Universal Turing Machine in MATLAB and then constructing the program data to meet the particular need... but what the person probably wanted was much easier to deal with; OR
[C] Should the volunteers take advantage of their experience to show the person asking the question what it is that the person really needed, and good practices towards accomplishing that goal?
When the volunteers, many of whom have decades of experience in programming, see someone who obviously does not understand the consequences of what they are asking, should the volunteers "butt out !!!" and give what was asked for, instead of what the volunteers could see was needed ? Or should the volunteers... well, you know, try to actually be helpful ?
.... I ask because the volunteers in this thread have been engaged in [C], trying to figure out what the person asking the question actually needed and advising the asker on good ways to accomplish those goals, but it is starting to feel like the volunteers are being told to do [A], only answer to exactly what was asked.
Walter Roberson
Walter Roberson 2021년 6월 19일
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 ?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by