how to handle 'nested' arrays

조회 수: 31(최근 30일)
maho 2017년 10월 9일
편집: Cedric Wannaz 2017년 10월 11일
Hello, I am new to MatLab, and am having a problem with code like this:
x=["bar" "foobar" "food" "bard"];
X is a string array, but the problem is y seems to be a cell array of string arrays- which I can't manipulate or use at all. So what I want to do is "simplify" y so that it is also a simple string array, by taking the first element of each element of the cell array. I.E., output should look like:
["" "foobar" "food" ""]
Any general references on how to handle and manipulate nested arrays of arrays would also be appreciated.

채택된 답변

Guillaume 2017년 10월 9일
In this particular instance, the easiest way to solve your problem is to add the 'once' option to your regexp:
matches = regexp(x, '.*foo.*', 'match', 'once');
  댓글 수: 6
Guillaume 2017년 10월 11일
which is best to use
That's a very easy question to answer: if you're on a version (R2016b or later) that support it and don't care about older versions, use string arrays. They're a lot more intuitive and powerful. strings didn't exist before R2016b so on earlier version char arrays where the only option.
You have to think of cell arrays as a container for any kind of things. The containers has cells where you put each things. You can look at the container itself, for example you can take the container that encompass rows 1:3 and columns 2:4. For that you use (), and you get another container:
container = cell(10, 20);
container_portion = container(1:3, 2:4); %container_portion is still a cell array
You can also looks at what's inside the container. That's when you use {}, you get whatever is inside each cell
container = cell(10, 20);
container{1, 5} %returns the content of cell(1,5)
You can ask for the content of several cells at once with {} and matlab returns a comma-separated list of the contents (see Cedric's Link), so if you do
you get a c-s-l of all the string arrays. You can pass that c-s-l to horzcat, which simply concatenate horizontally all its inputs, thus
extracts the content of all the cells of the cell array and converts into a row vector of this content.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

jean claude
jean claude 2017년 10월 9일
편집: jean claude 2017년 10월 9일
hi maho, i didn't understand what do you want exactly, but you can read here may be it helps!

Community Treasure Hunt

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

Start Hunting!

Translated by