Import of mixed string and numerical data from excel file
4 次查看(过去 30 天)
显示 更早的评论
I am currently trying to import data from an Excel file. The data typically looks like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/157612/image.png)
So far I have been dealing with only numerical tables with a limited number of arrays, but now I need to find certain data in large tables and prefer to automate the process a bit. In this regard I have a few problems with the import from Excel: 1. How can I read the column titled "Date & Time" as a string and convert its values into a numerical values? I already know that MATLAB can handle specific date and time formats and this seems to be outside of that format. 2. I want MATLAB to identify each column with the title stated in the first line (power, T, rpm etc.) so it can e.g. always know where the "force" data is stored and treat it accordingly? 3. How can I tell MATLAB to recognize the command * "Start Here"* in location (A11) so that I can e.g. start a certain calculation from this point on?
Thanks Saeid
0 个评论
采纳的回答
Mostafa
2016-11-2
I believe that the easiest (not the fastest) way is to import all the data at once in matlab environment then process it there.
DataSet = xlsread(fileName);
Analyze the first row of the imported data set: Search for certain strings, assign variables to the indicies of each data column (Power, T, rpm, force,..), and use these variables to deal with the rest of the data set.
IdxDate = find(cellfun(@(X) strcmp(X,'Date & Time'), DataSet(1,:)));
IdxPower = find(cellfun(@(X) strcmp(X, 'power'), DataSet(1,:)));
IdxForce = find(cellfun(@(X) strcmp(X, 'force'), DataSet(1,:)));
...
Analyze the first column of the imported data set: Search for the string "Start here" and start accessing the data set from this point.
IdxStart = find(cellfun(@(X) strcmp(X,'Start Here'), DataSet(:,1)));
%%Data of concern inside the power column
DataPower = DataSet(IdxStart:end, IdxPower);
%%Data of concern inside the force column
DataForce = DataSet(IdxStart:end, IdxForce);
%%Simple calculation
SomeValue = cellfun(@(X,Y) str2double(X)*str2double(Y), DataForce, DataPower);
%%Or if the data is parsed as numbers and not strings:
%%SomeValue = cellfun(@(X,Y) X*Y, DataForce, DataPower);
...
I'm not really sure what you mean by converting the date into numerical values, but you can use a similar notation.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!