Return a String value from my popup menu in MatLab - matlab

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?

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.

You can't use '+' to combine strings in matlab.
you can do:
disp(['serialPort',num2str(serialPortValue)]);

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

Related

Issue with eval_in_page - Trying to interpolate an array

my #para_text = $mech->xpath('/html/body/form/table/tbody/tr[2]/td[3]/table/tbody/tr[2]/td/table/tbody/tr[3]/td/div/div/div', type => $mech->xpathResult('STRING_TYPE'));
#BELOW IS JUST TO MAKE SURE THE ABOVE CAPTURED THE CORRECT TEXT
print "Look here: #para_text";
$mech->click_button( id => "lnkHdrreplyall");
$mech->eval_in_page('document.getElementsByName("txtbdy")[0].value = "#para_text"');
In the last line of my code I need to put the contents of the #para_text array as the text to output into a text box on a website however from the "document" till the end of the line it needs to be surrounded by ' ' to work. Obviously this doesnt allow interpolation as that would require " " Any ideas on what to do?
To define a string that itself contains double quotes as well as interpolating variable values, you may use the alternative form of the double quote qq/ ... /, where you can choose the delimiter yourself and prevent the double quote " from being special
So you can write
$mech->eval_in_page(qq/document.getElementsByName("txtbdy")[0].value = "#para_text"/)

MATLAB copyfile error: argument must contain a string

So I have a pretty simple problem I'm trying to solve. I want to create a backup of a file in MATLAB.
Here is my code (I am starting this script from my current directory):
backup_dir=strcat(pwd,'/backups/');
cd('../../source_destination/');
source_dir=pwd;
cd(backup_dir);
source_files=strcat(source_dir,'/*.m');
source_file_list=dir(source_files);
source_file_names={source_file_list.name}';
for i=1:numel(source_file_names)
source_file=strcat(source_dir,'/',source_file_names(i));
backup_file=strcat(backup_dir,source_file_names(i));
copyfile(source_file,backup_file);
end
Running this gives me the error:
Error using copyfile
Argument must contain a string.
However, when I actually examine source_file and backup_file, both variables return a valid string (enclosed by ' ') and both strings do point to a valid file:
>> source_file
source_file =
'/Users/me/mydir/cool/source_destination/archive.m'
>> backup_file
backup_file =
'/Users/me/mydir/cool/world/scripts/backups/archive.m'
Also, the actual content of source_file_list is valid.
So why would I be getting this error?
You need to dereference the cell array contents with curly braces, otherwise strcat returns a cell array of strings:
for i=1:numel(source_file_names)
source_file=strcat(source_dir,'/',source_file_names{i});
backup_file=strcat(backup_dir,source_file_names{i});
copyfile(source_file,backup_file);
end

Function to shuffle letters in words in matlab using randperm?? Changing from non-function format

I have the following to shuffle the letters in a word, but I need to change it into the form of a function that asks the user to input a word, and outputs the shuffled word.
How can I do this???
word = input('type a word to be scrambled: ', 's');
word(randperm(numel(word)))
The code you have to scramble the word is indeed correct. To do what you're asking, you really only have to make one change to the above code. Just place a function declaration in a .m file and call it something like scramble.m. Then do:
function word = scramble
word = input('type a word to be scrambled: ', 's');
word(randperm(numel(word)))
end
This should output the word as a string when you call the function. So save this file, then in the command prompt, type in:
>> word = scramble;
This should ask you for the word you want scrambled, scramble it and return the word. This word is stored in the variable word in the MATLAB workspace.
Some suggested reading for you: http://www.mathworks.com/help/matlab/ref/function.html
MathWorks is very good with their documentation and especially the syntax. Read the above link for further details on how you define a function and use it, but the gist of it is how I did it above.
The general format of a matlab function is
function output = MyFunctionName(input)
... code using 'input' goes here
end %
If you have multiple outputs, you place them inside an array, preferably separated by commas. If you have multiple inputs, you list them separated by commas:
function [out1, out2,...,outN] = MyFunctionName(input1, input2,...,inputN)
... code using the inputs goes here
end %
For your problem, you're not passing the word to the function so the call the function needs no input, BUT you need to input the word from inside the function. Here's one way to do it.
function word = ShuffleLetters
% Output: 'word' that is shuffled within function
% Input: none
word = input('type a word to be scrambled: ', 's');
word = word(randperm(numel(word)));
end
Here's a sample usage:
>> ShuffleLetters
type a word to be scrambled: bacon
ans =
bonca
Finally, inputs and outputs are optional. This m-function just prints 'Hi!'
function SayHi
disp('Hi!')
end

printing a vector of string in static text box with new lines

I have a bunch of classes that I am iterating through and collecting which classes the student is failing in. If the student fails , I collect the name of the class in a vector called retake.
retake =[Math History Science]
I have line breaks so when the classes print in the command window it shows as:
retake=
Math
History
Science.
However, I am trying display retake in a static text box in Gui Guide so it looks like the above. Instead, the static text box is showing as:
MathHistoryScience
set(handles.text13,'String', retake) % this is what I tried
can you please show me so it prints:
Math
History
Science
It looks to me like you need to add carriage returns.
Assuming you have a cell array with strings (rather than concatenated strings using [], which will give you a single long line), you can do it as follows:
retake = {'Math', 'History', 'Science'};
rString = '';
for ii = 1:numel(retake)-1
rString = [rString sprintf('%s\n', retake{ii}];
end
rString = [rString retake{end}];
Notice the use of '' to denote strings, {} to denote a cell array, '\n' as the end-of-line character, and [a b] to do simple string concatenation.

why strcat() doesn't return a string in Matlab?

I'm trying to access multiple files in a for loop, like this:
age = xlsread(strcat('Pipeline_BO_2013_',names(2),'_CDBU.xlsx'), 'Data', 'H:I')
It returns an error the filename must be string. So I did following test:
filename = strcat('Pipeline_BO_2013_',names(2),'_CDBU.xlsx')
filename =
'Pipeline_BO_2013_0107_CDBU.xlsx'
isstr(filename)
ans =
0
This is so weird. Could any one help me out? Thank you so much.
It looks like names is a cellstr and not a char array. If so, indexing in to it with parentheses like names(2) will return a 1-long cellstr array, not a char array. And when strcat is called with any of its arguments as a cellstr, it returns a cellstr. Then xlsread errors because it wants a char, not a cellstr.
Instead of just calling isstr or ischar on filename, do class(filename) and it'll tell you what it is.
Another clue is that filename is displayed with quotes. This is how cellstrs are displayed. If it were a char array, it would be displayed without quotes.
If this is the case, and names is a cellstr, you need to use {} indexing to "pop out" the cell contents.
filename = strcat('Pipeline_BO_2013_',names{2},'_CDBU.xlsx')
Or you can use sprintf, which you may find more readable, and will be more flexible once you start interpolating multiple arguments of different types.
filename = sprintf('Pipeline_BO_2013_%s_CDBU.xlsx', names{2})
% An example of more flexibility:
year = 2013;
filename = sprintf('Pipeline_BO_%04d_%s_CDBU.xlsx', year, names{2})