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

1 Ansicht (letzte 30 Tage)
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.

Antworten (1)

TallBrian
TallBrian am 4 Okt. 2016
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 Kommentare
TallBrian
TallBrian am 4 Okt. 2016
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

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by