Appending letters to number

4 vues (au cours des 30 derniers jours)
Umar Twahir
Umar Twahir le 7 Mar 2016
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

Réponses (2)

John BG
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
  1 commentaire
Umar Twahir
Umar Twahir le 8 Mar 2016
John
The issue at hand is the format of the value that I get out of the
setField = get(handles.edit1, 'String');
I have made a GUI to control a magnet field controller. I have it set up that you can enter the field value in the edit text box from the computer keyboard or a keypad in the GUI. And example of an entered value is 2000.
The command that has to be sent to the controller to set the field is
fprintf(g, 'CF2000'), with g referring to the gpib connection.
I do not want the user to have to enter CF before entering the field value so I am trying to use strcat to add it to the front of the value before executing the fprintf command. The command to the controller has to include single quotes around CF2000, so I tried to add the single quotes at the same time that I appended CF to the front of the value. The issue I was having at that step was that it would separate the into 'CF' '2000' as the output of strcat.
So I am trying to find a way to add CF to the front of the value back of the envelope and hopefully generate a new value such as
Field = 'CF2000' where 2000 is any input value. This way I can send the command in the format of
fprintf(g, Field)
Thanks in advance for any advice.
Umar

Connectez-vous pour commenter.


Steven Lord
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.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by