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.
0 件のコメント
回答 (1 件)
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
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 Center および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!