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

조회 수: 1 (최근 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에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by