How to update a value in a different location from a matrix after performing check at one location in MATLAB?

2 次查看(过去 30 天)
I have a matrix as follow.
file={'No','Question','Length';'1','how are you?','';'2','What is your name?','';'3','Where do you stay?','';'4','How old are you?',''}
I want to figure out the length for all the text columns but the returned length is always 1. Please advise. TQ.

回答(1 个)

TallBrian
TallBrian 2016-10-4
What you have is a cell array. If you want to do a function on every element of a cell array you can use cellfun. Here if you would like to calculate the length of each element of the cell array you could try the following code:
my_lengths = cellfun(@(x) length(x), file)
Here, x is each element of file treated individually.
  3 个评论
TallBrian
TallBrian 2016-10-4
Ken, it looks like you have not entered the command correctly. You need to enter the command as I indicated in the original answer. The error you receive seems to indicate you are trying to run length on raw(b,2), I am not sure what this is. You need to run length on x which is what cellfun creates for each element of file.
>> file={'No','Question','Length';'1','how are you?','';'2','What is your name?','';'3','Where do you stay?','';'4','How old are you?',''}
file =
'No' 'Question' 'Length'
'1' 'how are you?' ''
'2' 'What is your name?' ''
'3' 'Where do you stay?' ''
'4' 'How old are you?' ''
>> cellfun(@(x) length(x), file)
ans =
2 8 6
1 12 0
1 18 0
1 18 0
1 16 0

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by