Appending letters to number
Show older comments
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
Answers (2)
John BG
on 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
1 Comment
Umar Twahir
on 8 Mar 2016
Steven Lord
on 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.
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!