Extracting specified data from Excel
조회 수: 57 (최근 30일)
이전 댓글 표시
Hi! First of all: English is not my first language, but I hope I can describe my problem good enough for you. I have some excel sheets. These excel files contain several information like item number, date, measured value, test characteristics and so on. My final goal is to make several plots for every item number depending on the test characteristics (every item number has the same test characteristics) containing the measured values and the should be values. So my question is: Are there any possibilities I can realize that? The easiest would be, if there was a command I can type in the item number, test characteristic and maybe the date and matlab searches for the measured value and puts them in a vecor or matrix or something. I am pretty new with this and I really hope I could explain my problem well enough.
I am thankful for every input I can get.
thanks in advance
댓글 수: 3
dpb
2017년 4월 22일
While your English is really quite good, one educational point is in "I hope I can describe my problem good enough for you". This should be "well enough" rather. To see why, see--
Again, though, this is a pretty minor point in understanding but just as a sidebar... :)
채택된 답변
HaDu
2017년 4월 24일
댓글 수: 3
dpb
2017년 4월 24일
"I had to A = table2array(data) and plot..."
Shouldn't have to do that extra conversion, no. What release of Matlab are you using? At least by R2014b (and I think R2012b altho I didn't go back to make absolutely sure) plot was table-aware.
Read the section in the Examples under tables Access Data in a Table". If you use parentheses, the result is another table but similar to cell arrays curly braces or "dot indexing" will return the underlying data.
hL=plot(data{:,6:9})
should work directly.
추가 답변 (1개)
dpb
2017년 4월 21일
편집: dpb
2017년 4월 22일
OK, not a bad effort for first time... :)
IF the data are as the sample spreadsheet shows such that the uninteresting values are NaN and the four columns of interest are directly correlated as it appears they are, then
data=import(:,6:9); % pick up the area of interest
data=data(isfinite(data(:,1)),:); % save only those rows with finite values in first column
hL=plot(data); % plot the four columns
label(hL,'MW1','NM1','USG1','OSG1') % and label the lines
The "tricky" part above is that isfinite(data(:,1)) returns a logical vector that is True for the positions matching, False elsewhere and that vector is the row address in the subscripting expression for the data array and the ':' means all columns. Look up "logical indexing" in the documentation for more discussion on details, but is extremely important in how to use Matlab vector operations effectively which is one of (if not the) prime strengths.
One stylistic note, once you've set hold on, it can't get any "on"-er; no need to repeat that multiple times.
If the title row had useful names for all the columns, you could read those from the spreadsheet as well if you also returned the text and/or raw optional return variables from xlsread
ADDENDUM
Been tied up...here's a start using tables--
tab=readtable('example.xls'); % read in as a table
% Make variable names cleaner to be simpler to use
tab.Properties.VariableNames(1:6)={'Job','Number','Name','Date','Characteristic','Target'};
tab.Properties.VariableNames(9)={'Measured'};
% Remove NaN records
tab=tab(isfinite(tab.Target),:);
% convert appropriate variables to categorical and datetime...
for i=1:3,tab.(i)=categorical(tab{:,i});end
tab.Characteristic=categorical(...
cellfun(@(c) sscanf(char(c),'characteristic %d'), ...
tab.Characteristic));
tab.Date=datetime(tab.Date,'Format','MM/dd/yyyy hh:mm:ss a');
The above leaves a cleaned-up table to work with that looks like--
>> tab
tab =
Job Number Name Date Characteristic Target USG OSG Measured Var10 Var11 Var12 Var13
________ ______ ______ ______________________ ______________ ______ ____ _____ ________ _____ ____________ _____ _____
66403003 12345 Deckel 03/30/2017 02:06:37 AM 2 60 58.2 61.8 59.6 NaN 'plot these' NaN NaN
66403003 12345 Deckel 03/30/2017 02:06:36 AM 2 60 58.2 61.8 59.6 NaN '' NaN NaN
66403003 12345 Deckel 03/30/2017 02:06:34 AM 2 60 58.2 61.8 59.6 NaN '' NaN NaN
66403003 12345 Deckel 03/29/2017 01:38:58 PM 4 121 121 121.2 0 NaN '' NaN NaN
66500003 6789 Spule 03/24/2017 07:37:31 AM 6 22 21.6 22.4 22.2 NaN '' NaN NaN
66500003 6789 Spule 03/24/2017 07:37:29 AM 6 22 21.6 22.4 22.2 NaN '' NaN NaN
66500003 6789 Spule 03/24/2017 07:37:28 AM 6 22 21.6 22.4 22.2 NaN '' NaN NaN
66500003 6789 Spule 03/23/2017 10:48:54 PM 6 22 21.6 22.4 22.2 NaN '' NaN NaN
>>
Now you can do things like--
>> varfun(@mean,tab,'InputVariables',{'Target', 'Measured'},'GroupingVariables','Characteristic')
ans =
Characteristic GroupCount mean_Target mean_Measured
______________ __________ ___________ _____________
2 2 3 60 59.6
4 4 1 121 0
6 6 4 22 22.2
>>
Note the functional definition in varfun can be any function, not just a builtin as mean shown above.
댓글 수: 2
dpb
2017년 4월 21일
Well, I just duplicated your code using "more Matlab-y" syntax... :)
Sure, as noted, it looks like a table might be useful for the kinds of thinks you're after...
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!