Question

I am developing a simple GUI with MATLAB (guide) with a pop up menu in it. In order to establish a connection through a serial port.

function sendLog_OpeningFcn(hObject, eventdata, handles, varargin)
set(handles.popupmenuSerialPort,'String', {'''COM1''','''COM2''','''COM3''','''COM4'''});
...

I would like to get the selected value in this way:

serialPortList = get(handles.popupmenuSerialPort,'String');
    serialPortValue = get(handles.popupmenuSerialPort,'Value');
    serialPort = serialPortList(serialPortValue);
    disp('serialPort ' + serialPortValue);

But I get an error message on disp function:

Undefined function 'plus' for input arguments of type 'cell'.

Invalid PORT specified.

How could I get the chosen value?

Was it helpful?

Solution

I hate to plow through 2 answers that are clearly not bad, but here the devil is in the details. Yes, you cannot concatenate strings in MATLAB with the + operator, but the first red flag in your question is that your error message indicates a cell as one of the arguments to +. Note that disp has not even thrown an error at this point, it was +. This leads me to believe that your code is actually disp('serialPort ' + serialPort); not disp('serialPort ' + serialPortValue); since serialPortList is a cell array. Was this a typo?

So, by indexing it like serialPort = serialPortList(serialPortValue); you get a single cell in serialPort, which would not work with proper string concatenation or disp. The correction here is to use the curly braces ({}).

Together with valid string concatenation,

>> serialPort = serialPortList{serialPortValue};
>> disp(['serialPort ' serialPort])
serialPort 'COM3'

The single quotes are in the string because of how you set the strings with set(handles.popupmenuSerialPort,'String',..., so if you want to strip that, you can use strrep(serialPort,'''','').

Note that you can also use fprintf if you are more comfortable with that style of string formatting.

OTHER TIPS

You can't use '+' to combine strings in matlab. you can do:

disp(['serialPort',num2str(serialPortValue)]);

Try array concatenation :
disp(['SerialPort : ' serialPortValue]);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top