Basic cell array question

조회 수: 1 (최근 30일)
Ravi
Ravi 2012년 11월 16일
I have imported data that are mixed text and numeric values into a cell array.
Format of the data before import looks like
Filename(string) temperature(num) xdata(num) ydata(num) zdata(num)
I have made cell object datafile with the above as a header and data of the various types below the header. It is quite convenient to leave the header in so that I can keep track of fields within the variable browser.
There are several distinct values of temperatures for which I'd like to excerpt and process the xdata, ydata, and zdata
I know I can slice off the header row and the filename column and do a cell2mat to get a table of purely numerical values that I can process with normal array commands. That makes keeping track of column numbers a bit more tricky and I've trimmed reality for the example. There are many more columns to the right of zdata. Right now they are all numeric but I can imagine other datasets where there will be other datatypes.
Is there a way within the cell construct that I can do find operations on temperature values, pick out those rows matching some conditions on temperature, and then process?
e.g. 'meta code'
index = find in column 2 for temperature = 30 ; % Find rows matching condition
x = datafile(index,3); y = datafile(index,4); z = datafile(index,5);
process
plot
etc.
Since cells are not numeric, I've tried several different ways of referencing into the cell array and converting to numeric values but am getting errors of various types

채택된 답변

Matt J
Matt J 2012년 11월 16일
편집: Matt J 2012년 11월 16일
Well, first of all you wouldn't use FIND. You'd just use logical indexing:
index=[false, [data{2:end,2}]==30]; %ignore header by doing 2:end
x=data(index,3);
y=data(index,4);
z=data(index,5);
  댓글 수: 3
Matt J
Matt J 2012년 11월 16일
편집: Matt J 2012년 11월 16일
There's nothing wrong with FIND, if you really need non-logical indices for some reason, but there was no apparent need for it in your case.
You could have done this
index=find([data{2:end,2}]==30)+1;
Ravi
Ravi 2012년 11월 16일
Ahh, ok. When I invoked find, I did not think to use the square brackets. I was trying to match via
index=find(data{:,2}==30) ;
index=find(data{2:end,2}==30) ; etc.
and was getting Too many input arguments errors. I think this means I was getting a value out of the cell reference that was not a numeric type and hence the comparison to a numeric is not valid.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 11월 16일
x = cell2mat(datafile(index,3)); y = cell2mat(datafile(index,4)); z = cell2mat(datafile(index,5));

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by