Search code examples
matlabmatlab-figurematlab-deploymentmatlab-compiler

Matlab: How do I check the length string got more than certain number


I want to check the length of string got more than 20 characters, if more than 20 then will return 1 else return 0 in matrix form [n x 1]. But now, I get the answer of [1x1]. How do I modify my code in if-else statement to get the ans?

str = {'http://www.mathworks.com/matlabcentral/newsreader/view_thread/324182',
  'http://jitkomut.lecturer.eng.chula.ac.th/matlab/text.html',
  'http://www.ee.ic.ac.uk/pcheung/teaching/ee2_signals/Introduction%20to%20Matlab2.pdf'};

a = cellfun(@length,str)

if a > 20
'1'
else
'0'
end

Output: a =

68
57
83


ans =

1

I want the output of, lets say

ans = 

1
1
1

Solution

  • In Matlab, you can simply use (no if statement is needed):

    a = cellfun(@length,str)
    (a>20)'
    

    This will give you:

    a =
    
       68   57   83
    
    ans =
    
       1
       1
       1