Finding text - Can't locate where specific text is...

4 ビュー (過去 30 日間)
Adam
Adam 2011 年 5 月 18 日
I'm importing an excel file with cells that have values, and cells that are text.
Example
A | B | C
Noise | dB | 48
COx | V | 1.329
I import it as...
[data txt] = importxls('xxx.xls');
So data has my values and txt has the text cells. Note, text includes single quotation marks, so... 'Noise' 'COx'
Text repeats itself throughout the text matrix(?), and I would like to locate where in the array each unique text occurs, so that I can locate the corresponding value, because the order of the values is not always the same.
Help? Thanks.
PS - I feel like I ask very basic questions, and it may be a hassle for people to answer these types of questions. And I feel as though I spend a lot of time looking through the website, and other sites, for help.Does Mathworks have another way find solutions to simple problems? An easier way to find answers..?

回答 (3 件)

Walter Roberson
Walter Roberson 2011 年 5 月 18 日
See the multi-output form of unique()

Adam
Adam 2011 年 5 月 23 日
I found using 'strmatch' works for locating text in a matrix.
I first import an excel sheet in 2 parts (values & text) as...
[values text] = xlsread('filename.xls');
As in the example used above
txt_noise = strmatch('Noise',text(:,1));
and the output is an array with the locations, in this case, only 1 location,
txt_noise = 1
(but when there are many measurements, the array can be very long, and I can choose which one to use...)
and the value I can get by typing
noise = values(txt_noise(1))
= 48

Matt Tearle
Matt Tearle 2011 年 5 月 23 日
I don't know what you're planning to do with this data, but it seems like some of the data types available in Statistics Toolbox might be useful. In particular, you could convert text(:,1) to a nominal. Then you can do logical indexing, just like you would with numbers:
A = nominal(text(:,1));
idx = (A == 'Noise');
mean(values(idx))
Even better, you can do grouped operations using grpstats.
Aside 1: when you say importxls do you mean xlsread?
Aside 2: to answer your PS questions, you could always take one of our excellent training courses... :) [Disclaimer: I'm an not an objective observer!] MATLAB Fundamentals, MATLAB for Data Processing and Visualization, and Statistical Methods in MATLAB are some possibilities that might be useful to you.
EDIT TO ADD Without Stats TB, you could do
A = text(:,1);
idx = strcmp('Noise',A);
mean(values(idx))
  2 件のコメント
Adam
Adam 2011 年 5 月 23 日
I actually don't think I have the statistics toolbox, only the mapping toolbox for later on when I get passed this stage of the project... Or do all licenses come with the statistics toolbox?
Aside 1 -> I did mean xlsread...I just corrected it. I was just rushing to write my question. Thanks.
Aside 2 -> I have looked through, but have not been able to find the answers I need. I probably really just need to dedicate more time to going through them, however.
Thanks!
Matt Tearle
Matt Tearle 2011 年 5 月 23 日
Stats TB is not on all licenses, but it is very common. Try
which nominal
and see what the result is. Either way, I edited my answer to avoid using nominals.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by