How to update a value in a different location from a matrix after performing check at one location in MATLAB?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
TallBrian
il 4 Ott 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 Commenti
TallBrian
il 4 Ott 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
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!