如何在cellfun中选择函数的第二个参数

时间:2017-04-20 12:38:09

标签: matlab

我想拆分字符串的单元格数组,并采用第一个输出参数,如下所示

mycell={'a.1' 'b.2' 'c.3'}'
result1 = cellfun(@(x) strsplit(x,'.'),mycell,'UniformOutput',false)
result = cellfun(@(x) x{1},result1)

有没有办法在一行中执行操作,a.k.a在cellfun调用中指定参数1?

2 个答案:

答案 0 :(得分:2)

单行解决方案

你可以使用:

cellfun(@(x)subsref(strsplit(x,'.'),struct('type','{}','subs',ans = 
a
b
c
)),mycell);

<强>结果

04-20 14:51:57.139 8034-8034/com.example.irit.myapp E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.irit.myapp, PID: 8034
  java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
      at android.widget.AdapterView.addView(AdapterView.java:700)
      at com.example.irit.myapp.Model.ListObjet.creatView(ListObjet.java:65)
      at com.example.irit.myapp.Model.ListObjet.access$100(ListObjet.java:31)
      at com.example.irit.myapp.Model.ListObjet$1.onClick(ListObjet.java:50)
      at android.view.View.performClick(View.java:5697)
      at android.view.View$PerformClick.run(View.java:22526)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:158)
      at android.app.ActivityThread.main(ActivityThread.java:7224)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

答案 1 :(得分:1)

另一种选择是使用html, body { height: 100%; }

regexp

输出:

mycell = {'a.1' 'b.2' 'c.3'}';
result = regexp(mycell,'^[^.]+','match','once')