Appending letters to number
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I need to append CF in front of number to send a signal to a field controller. I need the output to be i.e. 'CF2000', with the single quotations.
my code is:
setField = get(handles.edit1, 'String');
centerField = 'CF'; %appends CF in front of field value
Field = strcat('',[centerField,setField],'');
g = handles.g;
fprintf(g, Field);
However when I use strcat I get 'CF' '2000'
Is there a way to fix this?
The final send command should read fprintf(g, 'CF2000') where the 2000 can be any number I set in the edit1 box.
Thanks
Umar
0 commentaires
Réponses (2)
John BG
le 7 Mar 2016
your syntax of strcat is not clear:
where you write
Field = strcat('',[centerField,setField],'')
you may want to write
Field = strcat(centerField,setField)
in your strcat, you are concatenating [' '] to [centerField setField] and to [' ']
Since you do not give details about setField, it's possible that as you are getting the handle, setField is a number. If you try to concatenate numbers and numbers into a solid string, use
num2str(setField)
or on any other numeric value before
strcat(str1,str2,str3, ..)
you don't really need the spaces do you?
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John
Steven Lord
le 8 Mar 2016
From your description, setField is not the char array '2000' but a cell array with one cell where the contents of that cell is the char array '2000'. Compare:
CH = '2000'
CA = {CH}
s1 = strcat('CF', CH)
s2 = strcat('CF', CA)
s3 = ['CF' CH]
s4 = ['CF' CA]
whos
Pay close attention to which of those variables are char arrays and which are cell arrays containing a char array. For purposes of FPRINTF, you're going to want to use one of the char arrays.
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!