Passing input variables to function
Afficher commentaires plus anciens
Hi,
I am working on a script in which I define variables that then get passed onto a function to do further calculations with.
I am able to pass numerical values across but when I try to pass on a string of say 'yellow' across to the function I get the following error message ' In an assignment A(I) = B, the number of elements in B and I must be the same.'
I was wondering if anyone could point me towards the right approach to take when passing text between Matlab function.
Many thanks in advance.
Hamza
4 commentaires
Mischa Kim
le 28 Avr 2014
Can you post the code or relevant code snippet?
dpb
le 28 Avr 2014
The problem likely doesn't have much (if anything) to do with passing the variable but in how you're trying to use it -- note the error is a mismatch in size between LHS and RHS of an assignment statement.
You likely are trying to stuff the full string consisting of multiple characters into a single memory location via the subscript.
Precisely how to solve the problem will depend on just what you're trying to accomplish so as Mischa says, post the problematical code so can see where you went wrong, specifically.
Hamza
le 28 Avr 2014
dpb
le 28 Avr 2014
Close enough... :)
Réponse acceptée
Plus de réponses (1)
Niklas Nylén
le 28 Avr 2014
Modifié(e) : Niklas Nylén
le 28 Avr 2014
The problem is that you are trying to put mixed data types (one number and one string) into the same vector. If you use a cell instead, by replacing () with {} this is possible:
number_jackets = 5
colour = 'yellow'
var{1} = number_jackets
var{2} = colour
You will need to use {} when accessing the elements again.
myJacketColor = var{2}
Another way is to pass them as two separate arguments
func = trial(number_jackets, colour)
1 commentaire
Hamza
le 28 Avr 2014
Catégories
En savoir plus sur Annotations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!