MATLAB Answers

0

handling arrays of strings

Asked by James Holland on 17 Feb 2015
Latest activity Edited by pankhuri kasliwal on 25 Jun 2019
I want to create an array of strings in a manner that allows the strings to be easily changed, I would like something like this:
Signals = cellstr('Signals');
horzcat(Signals,...
'SignalName1',...
'SignalName2',...
'SignalName3',...
'SignalName4',...
'SignalName5', );
The problem is that horzcat merges the strings, is there a simple way to achieve this? James

  4 Comments

Show 1 older comment
per isakson
on 17 Feb 2015
  • "create an array of strings" &nbsp is that cell array of strings?
  • "Signals = cellstr('Signals')" &nbsp what's the intent with this line?
  • "be easily changed" &nbsp is that easily easily edited in the editor?
The idea of the first line was to create a cell array with the header 'signals' that would then have the actual signal names appended. I must have done something wrong when I tried this because I was getting all the strings merged together; I have tried it again and it works correctly - I'll call that a senior moment. Thank you.
per isakson
on 17 Feb 2015
I often use this way to write cell arrays of strings in code. The string 'SignalName3' is commented out. It is easy to read and edit. Often I want a row to use in a for-loop: &nbsp for signal = Signals
Signals = {
'Signals'
'SignalName1'
'SignalName2'
... 'SignalName3'
'SignalName4'
'SignalName5'
};
Signals = transpose({
'Signals'
'SignalName1'
'SignalName2'
... 'SignalName3'
'SignalName4'
'SignalName5'
});

Sign in to comment.

1 Answer

Answer by pankhuri kasliwal on 25 Jun 2019
Edited by pankhuri kasliwal on 25 Jun 2019

Signals= cellstr('Signals');
Signals = [Signals, 'SignalName1'];
Signals = [Signals, 'SignalName2'];
This way it won't merge the strings all together, you will get all the strings in an array.

  0 Comments

Sign in to comment.