1

Two questions, one fairly simple question (at least it seems it should be simple) and one that may take a bit more work. Feel free to contribute to either or both.

First, I'd like to create a string array based off of an existing string array based on a criteria. Take for example a similar operation with a double array:

>> nums = [ 1 2 1 2]

nums =

     1     2     1     2

>> big_nums = (nums == 2) .* nums

big_nums =

     0     2     0     2

I'd like to do something similar with a string array, however I don't know what function to use:

>> sizes = ["XL" "L" "XL" "L"]

sizes = 

  1×4 string array

    "XL"    "L"    "XL"    "L"

>> large_sizes = (sizes == "L") .* sizes
Undefined operator '.*' for input arguments of type 'string'.

I'd like the output to be

large_sizes = 

  1×4 string array

    ""    "L"    ""    "L"

Second question. Suppose I have a 2 dimensional cell array. I'd like to filter data based on criteria:

>> data = {"winter", 1; "spring", 2; "summer", 3; "fall", 4}

data =

  4×2 cell array

    ["winter"]    [1]
    ["spring"]    [2]
    ["summer"]    [3]
    ["fall"  ]    [4]

>> nice_weather = ( (data(1,:) == "fall") + (data(1,:) == "spring") ) .* data
Error using  == 
Cell must be a cell array of character vectors.

I'd like a code that results in one of two arrays: nice_weather =

  4×2 cell array

    [""]          [1]
    ["spring"]    [2]
    [""]          [3]
    ["fall"]      [4]

----- OR -----

nice_weather =

  2×2 cell array

    ["spring"]    [2]
    ["fall"]      [4]

For this question, I am also open to separating data into multiple arrays (for example, one array for strings and one array for numbers).

Thanks!

1 Answer 1

1

This solution uses the strcmpi function from MATLAB (no toolbox required) to compare two strings (insensitive to the case).

1D Cell Array:

sizes = {'XL' 'L' 'XL' 'L'};    % Changed " to ' & used cell array
idx = strcmpi(sizes,'L');       % Logical index
sizelist = {sizes{idx}}

Or you could try something like
sizes(~idx) = {"" ""} % manual just for example

For this to automatically adjust the number of blanks "", you could use repmat like this sizes(~idx) = repmat({""},1,sum(~idx))

Output:

sizes = 1×4 cell array
{[""]} {'L'} {[""]} {'L'}

2D Cell Array:

data = {'winter', 1; 'spring', 2; 'summer', 3; 'fall', 4};   % Changed " to '
nicemo1 = 'spring';   
nicemo2 = 'fall';

idx = strcmpi(data(:,1),nicemo1) | strcmp(data(:,1),nicemo2);  % Obtain logical index
data(idx,:)

Output:

ans = 2×2 cell array
{'spring'} {[2]}
{'fall' } {[4]}


Tested with MATLAB R2018b.
Also beware variables like sizes as dropping a letter masks a useful function, size.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.