Move values based on string
Mostra commenti meno recenti
I need to import the following sheet from excel:

Then I want to move every value in column C to columns D, E and F based on results of string searches.
For example, I want to search column B (description) for 'gas' inside each cell and if it is present, move corresponding value in C (Value) to column D.
The same for strings 'Restaurant' and 'Hotel'. Note that the search should be case insensitive and matlab should take my parameter as an incomplete string, ie, imagine there was a Description HotelHereNow, then matlab should find it using parameter 'hotel'.
Of course this is just an example, real database is about 2000x10.
What is the best way to import the excel sheet: table, cell array? How can I search and move?
Risposte (1)
Walter Roberson
il 12 Nov 2016
For the searching part:
B_lower = lower(B); %to lowercase
contains_gas = ~cellfun(@isempty, strfind(B_lower, 'gas'));
D(contains_gas) = C(contains_gas);
for the reading part: readtable() would probably be good. You would then probably modify what I wrote above, to be (for example)
B_lower = lower(YourTable.Description); %to lowercase
contains_gas = ~cellfun(@isempty, strfind(B_lower, 'gas'));
YourTable.Gas(contains_gas) = YourTable.Value(contains_gas);
6 Commenti
Walter Roberson
il 13 Nov 2016
B_lower_descript = lower(sheet(:,2));
That is, my B assumed you had already assigned B the content of the second column.
Tfm tfm
il 13 Nov 2016
Walter Roberson
il 13 Nov 2016
B_lower_descript = cellfun(@lower, sheet(:,2));
Tfm tfm
il 13 Nov 2016
Walter Roberson
il 14 Nov 2016
B_lower_descript = cellfun(@lower, sheet(:,2), 'Uniform', 0);
Categorie
Scopri di più su Characters and Strings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!